<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="common/css/sf.css" rel="stylesheet" type="text/css" />
<title>Basics: Functional Programming in Coq</title>
<link href="common/jquery-ui/jquery-ui.css" rel="stylesheet">
<script src="common/jquery-ui/external/jquery/jquery.js"></script>
<script src="common/jquery-ui/jquery-ui.js"></script>
<script src="common/toggleproofs.js"></script>
<link href="common/css/lf.css" rel="stylesheet" type="text/css"/>
</head>

<body>

<div id="page">

<div id="header">
<div id='logoinheader'><a href='https://softwarefoundations.cis.upenn.edu'>
<img src='common/media/image/sf_logo_sm.png' alt='Software Foundations Logo'></a></div>
<div class='booktitleinheader'><a href='index.html'>Volume 1: Logical Foundations</a></div>
<ul id='menu'>
   <li class='section_name'><a href='toc.html'>Table of Contents</a></li>
   <li class='section_name'><a href='coqindex.html'>Index</a></li>
   <li class='section_name'><a href='deps.html'>Roadmap</a></li>
</ul>
</div>

<div id="main">

<h1 class="libtitle">Basics<span class="subtitle">Functional Programming in Coq</span></h1>



<div class="doc">
<a id="lab19"></a><h1 class="section">Introduction</h1>

<div class="paragraph"> </div>

 The functional style of programming is founded on simple, everyday
    mathematical intuition: If a procedure or method has no side
    effects, then (ignoring efficiency) all we need to understand
    about it is how it maps inputs to outputs -- that is, we can think
    of it as just a concrete method for computing a mathematical
    function.  This is one sense of the word "functional" in
    "functional programming."  The direct connection between programs
    and simple mathematical objects supports both formal correctness
    proofs and sound informal reasoning about program behavior.

<div class="paragraph"> </div>

    The other sense in which functional programming is "functional" is
    that it emphasizes the use of functions as <i>first-class</i> values --
    i.e., values that can be passed as arguments to other functions,
    returned as results, included in data structures, etc.  The
    recognition that functions can be treated as data gives rise to a
    host of useful and powerful programming idioms.

<div class="paragraph"> </div>

    Other common features of functional languages include <i>algebraic
    data types</i> and <i>pattern matching</i>, which make it easy to
    construct and manipulate rich data structures, and <i>polymorphic
    type systems</i> supporting abstraction and code reuse.  Coq offers
    all of these features.

<div class="paragraph"> </div>

    The first half of this chapter introduces the most essential
    elements of Coq's native functional programming language, called
    <i>Gallina</i>.  The second half introduces some basic <i>tactics</i> that
    can be used to prove properties of Gallina programs. 
</div>

<div class="doc">
<a id="lab20"></a><h1 class="section">Data and Functions</h1>

<div class="paragraph"> </div>

<a id="lab21"></a><h2 class="section">Enumerated Types</h2>

<div class="paragraph"> </div>

 One notable aspect of Coq is that its set of built-in
    features is <i>extremely</i> small.  For example, instead of providing
    the usual palette of atomic data types (booleans, integers,
    strings, etc.), Coq offers a powerful mechanism for defining new
    data types from scratch, with all these familiar types as
    instances.

<div class="paragraph"> </div>

    Naturally, the Coq distribution comes with an extensive standard
    library providing definitions of booleans, numbers, and many
    common data structures like lists and hash tables.  But there is
    nothing magic or primitive about these library definitions.  To
    illustrate this, this course we will explicitly recapitulate
    (almost) all the definitions we need, rather than getting them
    from the standard library. 
</div>

<div class="doc">
<a id="lab22"></a><h2 class="section">Days of the Week</h2>

<div class="paragraph"> </div>

 To see how this definition mechanism works, let's start with
    a very simple example.  The following declaration tells Coq that
    we are defining a set of data values -- a <i>type</i>. 
</div>
<div class="code">

<span class="id" title="keyword">Inductive</span> <a id="day" class="idref" href="#day"><span class="id" title="inductive">day</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
&nbsp;&nbsp;| <a id="monday" class="idref" href="#monday"><span class="id" title="constructor">monday</span></a><br/>
&nbsp;&nbsp;| <a id="tuesday" class="idref" href="#tuesday"><span class="id" title="constructor">tuesday</span></a><br/>
&nbsp;&nbsp;| <a id="wednesday" class="idref" href="#wednesday"><span class="id" title="constructor">wednesday</span></a><br/>
&nbsp;&nbsp;| <a id="thursday" class="idref" href="#thursday"><span class="id" title="constructor">thursday</span></a><br/>
&nbsp;&nbsp;| <a id="friday" class="idref" href="#friday"><span class="id" title="constructor">friday</span></a><br/>
&nbsp;&nbsp;| <a id="saturday" class="idref" href="#saturday"><span class="id" title="constructor">saturday</span></a><br/>
&nbsp;&nbsp;| <a id="sunday" class="idref" href="#sunday"><span class="id" title="constructor">sunday</span></a>.<br/>
</div>

<div class="doc">
The new type is called <span class="inlinecode"><span class="id" title="var">day</span></span>, and its members are <span class="inlinecode"><span class="id" title="var">monday</span></span>,
    <span class="inlinecode"><span class="id" title="var">tuesday</span></span>, etc.

<div class="paragraph"> </div>

    Having defined <span class="inlinecode"><span class="id" title="var">day</span></span>, we can write functions that operate on
    days. 
</div>
<div class="code">

<span class="id" title="keyword">Definition</span> <a id="next_weekday" class="idref" href="#next_weekday"><span class="id" title="definition">next_weekday</span></a> (<a id="d:3" class="idref" href="#d:3"><span class="id" title="binder">d</span></a>:<a class="idref" href="Basics.html#day"><span class="id" title="inductive">day</span></a>) : <a class="idref" href="Basics.html#day"><span class="id" title="inductive">day</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#d:3"><span class="id" title="variable">d</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#monday"><span class="id" title="constructor">monday</span></a>    ⇒ <a class="idref" href="Basics.html#tuesday"><span class="id" title="constructor">tuesday</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#tuesday"><span class="id" title="constructor">tuesday</span></a>   ⇒ <a class="idref" href="Basics.html#wednesday"><span class="id" title="constructor">wednesday</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#wednesday"><span class="id" title="constructor">wednesday</span></a> ⇒ <a class="idref" href="Basics.html#thursday"><span class="id" title="constructor">thursday</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#thursday"><span class="id" title="constructor">thursday</span></a>  ⇒ <a class="idref" href="Basics.html#friday"><span class="id" title="constructor">friday</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#friday"><span class="id" title="constructor">friday</span></a>    ⇒ <a class="idref" href="Basics.html#monday"><span class="id" title="constructor">monday</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#saturday"><span class="id" title="constructor">saturday</span></a>  ⇒ <a class="idref" href="Basics.html#monday"><span class="id" title="constructor">monday</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#sunday"><span class="id" title="constructor">sunday</span></a>    ⇒ <a class="idref" href="Basics.html#monday"><span class="id" title="constructor">monday</span></a><br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/>
</div>

<div class="doc">
One point to note is that the argument and return types of
    this function are explicitly declared.  Like most functional
    programming languages, Coq can often figure out these types for
    itself when they are not given explicitly -- i.e., it can do <i>type
    inference</i> -- but we'll generally include them to make reading
    easier. 
<div class="paragraph"> </div>

 Having defined a function, we should next check that it
    works on some examples.  There are actually three different ways
    to do the examples in Coq.  First, we can use the command
    <span class="inlinecode"><span class="id" title="keyword">Compute</span></span> to evaluate a compound expression involving
    <span class="inlinecode"><span class="id" title="var">next_weekday</span></span>. 
</div>
<div class="code">

<span class="id" title="keyword">Compute</span> (<a class="idref" href="Basics.html#next_weekday"><span class="id" title="definition">next_weekday</span></a> <a class="idref" href="Basics.html#friday"><span class="id" title="constructor">friday</span></a>).<br/>
<span class="comment">(*&nbsp;==&gt;&nbsp;monday&nbsp;:&nbsp;day&nbsp;*)</span><br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Compute</span> (<a class="idref" href="Basics.html#next_weekday"><span class="id" title="definition">next_weekday</span></a> (<a class="idref" href="Basics.html#next_weekday"><span class="id" title="definition">next_weekday</span></a> <a class="idref" href="Basics.html#saturday"><span class="id" title="constructor">saturday</span></a>)).<br/>
<span class="comment">(*&nbsp;==&gt;&nbsp;tuesday&nbsp;:&nbsp;day&nbsp;*)</span><br/>
</div>

<div class="doc">
(We show Coq's responses in comments, but, if you have a
    computer handy, this would be an excellent moment to fire up the
    Coq interpreter under your favorite IDE -- either CoqIde or Proof
    General -- and try it for yourself.  Load this file, <span class="inlinecode"><span class="id" title="var">Basics.v</span></span>,
    from the book's Coq sources, find the above example, submit it to
    Coq, and observe the result.) 
<div class="paragraph"> </div>

 Second, we can record what we <i>expect</i> the result to be in the
    form of a Coq example: 
</div>
<div class="code">

<span class="id" title="keyword">Example</span> <a id="test_next_weekday" class="idref" href="#test_next_weekday"><span class="id" title="definition">test_next_weekday</span></a>:<br/>
&nbsp;&nbsp;<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#next_weekday"><span class="id" title="definition">next_weekday</span></a> (<a class="idref" href="Basics.html#next_weekday"><span class="id" title="definition">next_weekday</span></a> <a class="idref" href="Basics.html#saturday"><span class="id" title="constructor">saturday</span></a>)<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#tuesday"><span class="id" title="constructor">tuesday</span></a>.<br/>
</div>

<div class="doc">
This declaration does two things: it makes an
    assertion (that the second weekday after <span class="inlinecode"><span class="id" title="var">saturday</span></span> is <span class="inlinecode"><span class="id" title="var">tuesday</span></span>),
    and it gives the assertion a name that can be used to refer to it
    later.  Having made the assertion, we can also ask Coq to verify
    it like this: 
</div>
<div class="code">

<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
The details are not important just now, but essentially this
    can be read as "The assertion we've just made can be proved by
    observing that both sides of the equality evaluate to the same
    thing."

<div class="paragraph"> </div>

    Third, we can ask Coq to <i>extract</i>, from our <span class="inlinecode"><span class="id" title="keyword">Definition</span></span>, a
    program in another, more conventional, programming
    language (OCaml, Scheme, or Haskell) with a high-performance
    compiler.  This facility is very interesting, since it gives us a
    path from proved-correct algorithms written in Gallina to
    efficient machine code.  (Of course, we are trusting the
    correctness of the OCaml/Haskell/Scheme compiler, and of Coq's
    extraction facility itself, but this is still a big step forward
    from the way most software is developed today.) Indeed, this is
    one of the main uses for which Coq was developed.  We'll come back
    to this topic in later chapters. 
</div>

<div class="doc">
<a id="lab23"></a><h2 class="section">Homework Submission Guidelines</h2>

<div class="paragraph"> </div>

 If you are using <i>Software Foundations</i> in a course, your
    instructor may use automatic scripts to help grade your homework
    assignments.  In order for these scripts to work correctly (and
    give you that you get full credit for your work!), please be
    careful to follow these rules:
<ul class="doclist">
<li> The grading scripts work by extracting marked regions of the
        <span class="inlinecode">.<span class="id" title="var">v</span></span> files that you submit.  It is therefore important that
        you do not alter the "markup" that delimits exercises: the
        Exercise header, the name of the exercise, the "empty square
        bracket" marker at the end, etc.  Please leave this markup
        exactly as you find it.

</li>
<li> Do not delete exercises.  If you skip an exercise (e.g.,
        because it is marked "optional," or because you can't solve it),
        it is OK to leave a partial proof in your <span class="inlinecode">.<span class="id" title="var">v</span></span> file; in
        this case, please make sure it ends with <span class="inlinecode"><span class="id" title="var">Admitted</span></span> (not, for
        example <span class="inlinecode"><span class="id" title="keyword">Abort</span></span>).

</li>
<li> It is fine to use additional definitions (of helper functions,
        useful lemmas, etc.) in your solutions.  You can put these
        between the exercise header and the theorem you are asked to
        prove.

</li>
<li> If you introduce a helper lemma that you end up being unable
        to prove, hence end it with <span class="inlinecode"><span class="id" title="var">Admitted</span></span>, then make sure to also
        end the main theorem in which you use it with <span class="inlinecode"><span class="id" title="var">Admitted</span></span>, not
        <span class="inlinecode"><span class="id" title="keyword">Qed</span></span>.  That will help you get partial credit, in case you
        use that main theorem to solve a later exercise.

</li>
</ul>

<div class="paragraph"> </div>

    You will also notice that each chapter (like <span class="inlinecode"><span class="id" title="var">Basics.v</span></span>) is
    accompanied by a <i>test script</i> (<span class="inlinecode"><span class="id" title="var">BasicsTest.v</span></span>) that automatically
    calculates points for the finished homework problems in the
    chapter.  These scripts are mostly for the auto-grading
    tools, but you may also want to use them to double-check
    that your file is well formatted before handing it in.  In a
    terminal window, either type "<span class="inlinecode"><span class="id" title="var">make</span></span> <span class="inlinecode"><span class="id" title="var">BasicsTest.vo</span></span>" or do the
    following:
<br/>
<span class="inlinecode">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="id" title="var">coqc</span> -<span class="id" title="var">Q</span> . <span class="id" title="var">LF</span> <span class="id" title="var">Basics.v</span><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="id" title="var">coqc</span> -<span class="id" title="var">Q</span> . <span class="id" title="var">LF</span> <span class="id" title="var">BasicsTest.v</span>
</span>    See the end of this chapter for more information about how to interpret
    the output of test scripts.

<div class="paragraph"> </div>

    There is no need to hand in <span class="inlinecode"><span class="id" title="var">BasicsTest.v</span></span> itself (or <span class="inlinecode"><span class="id" title="var">Preface.v</span></span>).

<div class="paragraph"> </div>

    If your class is using the Canvas system to hand in assignments...
<ul class="doclist">
<li> If you submit multiple versions of the assignment, you may
        notice that they are given different names.  This is fine: The
        most recent submission is the one that will be graded.

</li>
<li> To hand in multiple files at the same time (if more than one
        chapter is assigned in the same week), you need to make a
        single submission with all the files at once using the button
        "Add another file" just above the comment box. 
</li>
</ul>

<div class="paragraph"> </div>

 The <span class="inlinecode"><span class="id" title="keyword">Require</span></span> <span class="inlinecode"><span class="id" title="keyword">Export</span></span> statement on the next line tells Coq to use
    the <span class="inlinecode"><span class="id" title="var">String</span></span> module from the standard library.  We'll use strings
    ourselves in later chapters, but we need to <span class="inlinecode"><span class="id" title="keyword">Require</span></span> it here so
    that the grading scripts can use it for internal purposes. 
</div>
<div class="code">
<span class="id" title="keyword">From</span> <span class="id" title="var">Coq</span> <span class="id" title="keyword">Require</span> <span class="id" title="keyword">Export</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Strings.String.html#"><span class="id" title="library">String</span></a>.<br/>
</div>

<div class="doc">
<a id="lab24"></a><h2 class="section">Booleans</h2>

<div class="paragraph"> </div>

 In a similar way, we can define the standard type <span class="inlinecode"><span class="id" title="var">bool</span></span> of
    booleans, with members <span class="inlinecode"><span class="id" title="var">true</span></span> and <span class="inlinecode"><span class="id" title="var">false</span></span>. 
</div>
<div class="code">

<span class="id" title="keyword">Inductive</span> <a id="bool" class="idref" href="#bool"><span class="id" title="inductive">bool</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
&nbsp;&nbsp;| <a id="true" class="idref" href="#true"><span class="id" title="constructor">true</span></a><br/>
&nbsp;&nbsp;| <a id="false" class="idref" href="#false"><span class="id" title="constructor">false</span></a>.<br/>
</div>

<div class="doc">
Functions over booleans can be defined in the same way as
    above: 
</div>
<div class="code">

<span class="id" title="keyword">Definition</span> <a id="negb" class="idref" href="#negb"><span class="id" title="definition">negb</span></a> (<a id="b:7" class="idref" href="#b:7"><span class="id" title="binder">b</span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#b:7"><span class="id" title="variable">b</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> ⇒ <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Definition</span> <a id="andb" class="idref" href="#andb"><span class="id" title="definition">andb</span></a> (<a id="b<sub>1</sub>:9" class="idref" href="#b<sub>1</sub>:9"><span class="id" title="binder">b<sub>1</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) (<a id="b<sub>2</sub>:10" class="idref" href="#b<sub>2</sub>:10"><span class="id" title="binder">b<sub>2</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#b<sub>1</sub>:9"><span class="id" title="variable">b<sub>1</sub></span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> ⇒ <a class="idref" href="Basics.html#b<sub>2</sub>:10"><span class="id" title="variable">b<sub>2</sub></span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Definition</span> <a id="orb" class="idref" href="#orb"><span class="id" title="definition">orb</span></a> (<a id="b<sub>1</sub>:12" class="idref" href="#b<sub>1</sub>:12"><span class="id" title="binder">b<sub>1</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) (<a id="b<sub>2</sub>:13" class="idref" href="#b<sub>2</sub>:13"><span class="id" title="binder">b<sub>2</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#b<sub>1</sub>:12"><span class="id" title="variable">b<sub>1</sub></span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> ⇒ <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> ⇒ <a class="idref" href="Basics.html#b<sub>2</sub>:13"><span class="id" title="variable">b<sub>2</sub></span></a><br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/>
</div>

<div class="doc">
(Although we are rolling our own booleans here for the sake
    of building up everything from scratch, Coq does, of course,
    provide a default implementation of the booleans, together with a
    multitude of useful functions and lemmas.  Whenever possible,
    we'll name our own definitions and theorems so that they exactly
    coincide with the ones in the standard library.) 
<div class="paragraph"> </div>

 The last two of these illustrate Coq's syntax for
    multi-argument function definitions.  The corresponding
    multi-argument application syntax is illustrated by the following
    "unit tests," which constitute a complete specification -- a truth
    table -- for the <span class="inlinecode"><span class="id" title="var">orb</span></span> function: 
</div>
<div class="code">

<span class="id" title="keyword">Example</span> <a id="test_orb1" class="idref" href="#test_orb1"><span class="id" title="definition">test_orb1</span></a>:  <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#orb"><span class="id" title="definition">orb</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>  <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_orb2" class="idref" href="#test_orb2"><span class="id" title="definition">test_orb2</span></a>:  <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#orb"><span class="id" title="definition">orb</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_orb3" class="idref" href="#test_orb3"><span class="id" title="definition">test_orb3</span></a>:  <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#orb"><span class="id" title="definition">orb</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a>  <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_orb4" class="idref" href="#test_orb4"><span class="id" title="definition">test_orb4</span></a>:  <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#orb"><span class="id" title="definition">orb</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>  <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a>  <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
We can also introduce some familiar infix syntax for the
    boolean operations we have just defined. The <span class="inlinecode"><span class="id" title="keyword">Notation</span></span> command
    defines a new symbolic notation for an existing definition. 
</div>
<div class="code">

<span class="id" title="keyword">Notation</span> <a id=":::x_'&amp;&amp;'_x" class="idref" href="#:::x_'&amp;&amp;'_x"><span class="id" title="notation">&quot;</span></a>x &amp;&amp; y" := (<a class="idref" href="Basics.html#andb"><span class="id" title="definition">andb</span></a> <span class="id" title="var">x</span> <span class="id" title="var">y</span>).<br/>
<span class="id" title="keyword">Notation</span> <a id="34428c8531fd724748c0ebf19a35c764" class="idref" href="#34428c8531fd724748c0ebf19a35c764"><span class="id" title="notation">&quot;</span></a>x || y" := (<a class="idref" href="Basics.html#orb"><span class="id" title="definition">orb</span></a> <span class="id" title="var">x</span> <span class="id" title="var">y</span>).<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="test_orb5" class="idref" href="#test_orb5"><span class="id" title="definition">test_orb5</span></a>:  <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> <a class="idref" href="Basics.html#34428c8531fd724748c0ebf19a35c764"><span class="id" title="notation">||</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> <a class="idref" href="Basics.html#34428c8531fd724748c0ebf19a35c764"><span class="id" title="notation">||</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
<i>A note on notation</i>: In <span class="inlinecode">.<span class="id" title="var">v</span></span> files, we use square brackets
    to delimit fragments of Coq code within comments; this convention,
    also used by the <span class="inlinecode"><span class="id" title="var">coqdoc</span></span> documentation tool, keeps them visually
    separate from the surrounding text.  In the HTML version of the
    files, these pieces of text appear in a <span class="inlinecode"><span class="id" title="var">different</span></span> <span class="inlinecode"><span class="id" title="var">font</span></span>. 
<div class="paragraph"> </div>

 These examples are also an opportunity to introduce one more small
    feature of Coq's programming language: conditional expressions... 
</div>
<div class="code">

<span class="id" title="keyword">Definition</span> <a id="negb'" class="idref" href="#negb'"><span class="id" title="definition">negb'</span></a> (<a id="b:15" class="idref" href="#b:15"><span class="id" title="binder">b</span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">if</span> <a class="idref" href="Basics.html#b:15"><span class="id" title="variable">b</span></a> <span class="id" title="keyword">then</span> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
&nbsp;&nbsp;<span class="id" title="keyword">else</span> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Definition</span> <a id="andb'" class="idref" href="#andb'"><span class="id" title="definition">andb'</span></a> (<a id="b<sub>1</sub>:16" class="idref" href="#b<sub>1</sub>:16"><span class="id" title="binder">b<sub>1</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) (<a id="b<sub>2</sub>:17" class="idref" href="#b<sub>2</sub>:17"><span class="id" title="binder">b<sub>2</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">if</span> <a class="idref" href="Basics.html#b<sub>1</sub>:16"><span class="id" title="variable">b<sub>1</sub></span></a> <span class="id" title="keyword">then</span> <a class="idref" href="Basics.html#b<sub>2</sub>:17"><span class="id" title="variable">b<sub>2</sub></span></a><br/>
&nbsp;&nbsp;<span class="id" title="keyword">else</span> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Definition</span> <a id="orb'" class="idref" href="#orb'"><span class="id" title="definition">orb'</span></a> (<a id="b<sub>1</sub>:18" class="idref" href="#b<sub>1</sub>:18"><span class="id" title="binder">b<sub>1</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) (<a id="b<sub>2</sub>:19" class="idref" href="#b<sub>2</sub>:19"><span class="id" title="binder">b<sub>2</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">if</span> <a class="idref" href="Basics.html#b<sub>1</sub>:18"><span class="id" title="variable">b<sub>1</sub></span></a> <span class="id" title="keyword">then</span> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
&nbsp;&nbsp;<span class="id" title="keyword">else</span> <a class="idref" href="Basics.html#b<sub>2</sub>:19"><span class="id" title="variable">b<sub>2</sub></span></a>.<br/>
</div>

<div class="doc">
Coq's conditionals are exactly like those found in any other
    language, with one small generalization.  Since the <span class="inlinecode"><span class="id" title="var">bool</span></span> type is
    not built in, Coq actually supports conditional expressions over
    <i>any</i> inductively defined type with exactly two clauses in its
    definition.  The guard is considered true if it evaluates to the
    "constructor" of the first clause of the <span class="inlinecode"><span class="id" title="keyword">Inductive</span></span>
    definition (which just happens to be called <span class="inlinecode"><span class="id" title="var">true</span></span> in this case)
    and false if it evaluates to the second. 
<div class="paragraph"> </div>

<a id="lab25"></a><h4 class="section">Exercise: 1 star, standard (nandb)</h4>
 The command <span class="inlinecode"><span class="id" title="var">Admitted</span></span> can be used as a placeholder for an
    incomplete proof.  We use it in exercises to indicate the parts
    that we're leaving for you -- i.e., your job is to replace
    <span class="inlinecode"><span class="id" title="var">Admitted</span></span>s with real proofs.

<div class="paragraph"> </div>

    Remove "<span class="inlinecode"><span class="id" title="var">Admitted</span>.</span>" and complete the definition of the following
    function; then make sure that the <span class="inlinecode"><span class="id" title="keyword">Example</span></span> assertions below can
    each be verified by Coq.  (I.e., fill in each proof, following the
    model of the <span class="inlinecode"><span class="id" title="var">orb</span></span> tests above, and make sure Coq accepts it.) The
    function should return <span class="inlinecode"><span class="id" title="var">true</span></span> if either or both of its inputs are
    <span class="inlinecode"><span class="id" title="var">false</span></span>. 
</div>
<div class="code">

<span class="id" title="keyword">Definition</span> <a id="nandb" class="idref" href="#nandb"><span class="id" title="definition">nandb</span></a> (<a id="b<sub>1</sub>:20" class="idref" href="#b<sub>1</sub>:20"><span class="id" title="binder">b<sub>1</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) (<a id="b<sub>2</sub>:21" class="idref" href="#b<sub>2</sub>:21"><span class="id" title="binder">b<sub>2</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a><br/>
&nbsp;&nbsp;<span class="comment">(*&nbsp;REPLACE&nbsp;THIS&nbsp;LINE&nbsp;WITH&nbsp;":=&nbsp;_your_definition_&nbsp;."&nbsp;*)</span>. <span class="id" title="var">Admitted</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="test_nandb1" class="idref" href="#test_nandb1"><span class="id" title="definition">test_nandb1</span></a>:               <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#nandb"><span class="id" title="axiom">nandb</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_nandb2" class="idref" href="#test_nandb2"><span class="id" title="definition">test_nandb2</span></a>:               <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#nandb"><span class="id" title="axiom">nandb</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_nandb3" class="idref" href="#test_nandb3"><span class="id" title="definition">test_nandb3</span></a>:               <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#nandb"><span class="id" title="axiom">nandb</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_nandb4" class="idref" href="#test_nandb4"><span class="id" title="definition">test_nandb4</span></a>:               <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#nandb"><span class="id" title="axiom">nandb</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>&#9744;</font>
</div>

<div class="doc"> 
<div class="paragraph"> </div>

<a id="lab26"></a><h4 class="section">Exercise: 1 star, standard (andb3)</h4>
 Do the same for the <span class="inlinecode"><span class="id" title="var">andb3</span></span> function below. This function should
    return <span class="inlinecode"><span class="id" title="var">true</span></span> when all of its inputs are <span class="inlinecode"><span class="id" title="var">true</span></span>, and <span class="inlinecode"><span class="id" title="var">false</span></span>
    otherwise. 
</div>
<div class="code">

<span class="id" title="keyword">Definition</span> <a id="andb3" class="idref" href="#andb3"><span class="id" title="definition">andb3</span></a> (<a id="b<sub>1</sub>:22" class="idref" href="#b<sub>1</sub>:22"><span class="id" title="binder">b<sub>1</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) (<a id="b<sub>2</sub>:23" class="idref" href="#b<sub>2</sub>:23"><span class="id" title="binder">b<sub>2</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) (<a id="b<sub>3</sub>:24" class="idref" href="#b<sub>3</sub>:24"><span class="id" title="binder">b<sub>3</sub></span></a>:<a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a><br/>
&nbsp;&nbsp;<span class="comment">(*&nbsp;REPLACE&nbsp;THIS&nbsp;LINE&nbsp;WITH&nbsp;":=&nbsp;_your_definition_&nbsp;."&nbsp;*)</span>. <span class="id" title="var">Admitted</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="test_andb31" class="idref" href="#test_andb31"><span class="id" title="definition">test_andb31</span></a>:                 <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#andb3"><span class="id" title="axiom">andb3</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_andb32" class="idref" href="#test_andb32"><span class="id" title="definition">test_andb32</span></a>:                 <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#andb3"><span class="id" title="axiom">andb3</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_andb33" class="idref" href="#test_andb33"><span class="id" title="definition">test_andb33</span></a>:                 <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#andb3"><span class="id" title="axiom">andb3</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_andb34" class="idref" href="#test_andb34"><span class="id" title="definition">test_andb34</span></a>:                 <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#andb3"><span class="id" title="axiom">andb3</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>&#9744;</font>
</div>


<div class="doc">
<a id="lab27"></a><h2 class="section">Types</h2>

<div class="paragraph"> </div>

 Every expression in Coq has a type, describing what sort of
    thing it computes. The <span class="inlinecode"><span class="id" title="keyword">Check</span></span> command asks Coq to print the type
    of an expression. 
</div>
<div class="code">

<span class="id" title="keyword">Check</span> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="comment">(*&nbsp;===&gt;&nbsp;true&nbsp;:&nbsp;bool&nbsp;*)</span><br/>
</div>

<div class="doc">
If the expression after <span class="inlinecode"><span class="id" title="keyword">Check</span></span> is followed by a colon and a type,
    Coq will verify that the type of the expression matches the given
    type and halt with an error if not. 
</div>
<div class="code">

<span class="id" title="keyword">Check</span> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
&nbsp;&nbsp;: <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>.<br/>
<span class="id" title="keyword">Check</span> (<a class="idref" href="Basics.html#negb"><span class="id" title="definition">negb</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>)<br/>
&nbsp;&nbsp;: <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>.<br/>
</div>

<div class="doc">
Functions like <span class="inlinecode"><span class="id" title="var">negb</span></span> itself are also data values, just like
    <span class="inlinecode"><span class="id" title="var">true</span></span> and <span class="inlinecode"><span class="id" title="var">false</span></span>.  Their types are called <i>function types</i>, and
    they are written with arrows. 
</div>
<div class="code">

<span class="id" title="keyword">Check</span> <a class="idref" href="Basics.html#negb"><span class="id" title="definition">negb</span></a><br/>
&nbsp;&nbsp;: <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'-&gt;'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>.<br/>
</div>

<div class="doc">
The type of <span class="inlinecode"><span class="id" title="var">negb</span></span>, written <span class="inlinecode"><span class="id" title="var">bool</span></span> <span class="inlinecode">→</span> <span class="inlinecode"><span class="id" title="var">bool</span></span> and pronounced
    "<span class="inlinecode"><span class="id" title="var">bool</span></span> arrow <span class="inlinecode"><span class="id" title="var">bool</span></span>," can be read, "Given an input of type
    <span class="inlinecode"><span class="id" title="var">bool</span></span>, this function produces an output of type <span class="inlinecode"><span class="id" title="var">bool</span></span>."
    Similarly, the type of <span class="inlinecode"><span class="id" title="var">andb</span></span>, written <span class="inlinecode"><span class="id" title="var">bool</span></span> <span class="inlinecode">→</span> <span class="inlinecode"><span class="id" title="var">bool</span></span> <span class="inlinecode">→</span> <span class="inlinecode"><span class="id" title="var">bool</span></span>, can
    be read, "Given two inputs, each of type <span class="inlinecode"><span class="id" title="var">bool</span></span>, this function
    produces an output of type <span class="inlinecode"><span class="id" title="var">bool</span></span>." 
</div>

<div class="doc">
<a id="lab28"></a><h2 class="section">New Types from Old</h2>

<div class="paragraph"> </div>

 The types we have defined so far are examples of "enumerated
    types": their definitions explicitly enumerate a finite set of
    elements, called <i>constructors</i>.  Here is a more interesting type
    definition, where one of the constructors takes an argument: 
</div>
<div class="code">

<span class="id" title="keyword">Inductive</span> <a id="rgb" class="idref" href="#rgb"><span class="id" title="inductive">rgb</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
&nbsp;&nbsp;| <a id="red" class="idref" href="#red"><span class="id" title="constructor">red</span></a><br/>
&nbsp;&nbsp;| <a id="green" class="idref" href="#green"><span class="id" title="constructor">green</span></a><br/>
&nbsp;&nbsp;| <a id="blue" class="idref" href="#blue"><span class="id" title="constructor">blue</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Inductive</span> <a id="color" class="idref" href="#color"><span class="id" title="inductive">color</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
&nbsp;&nbsp;| <a id="black" class="idref" href="#black"><span class="id" title="constructor">black</span></a><br/>
&nbsp;&nbsp;| <a id="white" class="idref" href="#white"><span class="id" title="constructor">white</span></a><br/>
&nbsp;&nbsp;| <a id="primary" class="idref" href="#primary"><span class="id" title="constructor">primary</span></a> (<a id="p:29" class="idref" href="#p:29"><span class="id" title="binder">p</span></a> : <a class="idref" href="Basics.html#rgb"><span class="id" title="inductive">rgb</span></a>).<br/>
</div>

<div class="doc">
Let's look at this in a little more detail.

<div class="paragraph"> </div>

    An <span class="inlinecode"><span class="id" title="keyword">Inductive</span></span> definition does two things:

<div class="paragraph"> </div>

<ul class="doclist">
<li> It defines a set of new <i>constructors</i>. E.g., <span class="inlinecode"><span class="id" title="tactic">red</span></span>,
      <span class="inlinecode"><span class="id" title="var">primary</span></span>, <span class="inlinecode"><span class="id" title="var">true</span></span>, <span class="inlinecode"><span class="id" title="var">false</span></span>, <span class="inlinecode"><span class="id" title="var">monday</span></span>, etc. are constructors.

<div class="paragraph"> </div>


</li>
<li> It groups them into a new named type, like <span class="inlinecode"><span class="id" title="var">bool</span></span>, <span class="inlinecode"><span class="id" title="var">rgb</span></span>, or
      <span class="inlinecode"><span class="id" title="var">color</span></span>.

</li>
</ul>

<div class="paragraph"> </div>

    <i>Constructor expressions</i> are formed by applying a constructor
    to zero or more other constructors or constructor expressions,
    obeying the declared number and types of the constructor arguments.
    E.g.,
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" title="tactic">red</span></span>

</li>
<li> <span class="inlinecode"><span class="id" title="var">true</span></span>

</li>
<li> <span class="inlinecode"><span class="id" title="var">primary</span></span> <span class="inlinecode"><span class="id" title="tactic">red</span></span>

</li>
<li> etc.

</li>
</ul>
    But not
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" title="tactic">red</span></span> <span class="inlinecode"><span class="id" title="var">primary</span></span>

</li>
<li> <span class="inlinecode"><span class="id" title="var">true</span></span> <span class="inlinecode"><span class="id" title="tactic">red</span></span>

</li>
<li> <span class="inlinecode"><span class="id" title="var">primary</span></span> <span class="inlinecode">(<span class="id" title="var">primary</span></span> <span class="inlinecode"><span class="id" title="tactic">red</span>)</span>

</li>
<li> etc.

</li>
</ul>

<div class="paragraph"> </div>

 In particular, the definitions of <span class="inlinecode"><span class="id" title="var">rgb</span></span> and <span class="inlinecode"><span class="id" title="var">color</span></span> say
    which constructor expressions belong to the sets <span class="inlinecode"><span class="id" title="var">rgb</span></span> and
    <span class="inlinecode"><span class="id" title="var">color</span></span>:

<div class="paragraph"> </div>

<ul class="doclist">
<li> <span class="inlinecode"><span class="id" title="tactic">red</span></span>, <span class="inlinecode"><span class="id" title="var">green</span></span>, and <span class="inlinecode"><span class="id" title="var">blue</span></span> belong to the set <span class="inlinecode"><span class="id" title="var">rgb</span></span>;

</li>
<li> <span class="inlinecode"><span class="id" title="var">black</span></span> and <span class="inlinecode"><span class="id" title="var">white</span></span> belong to the set <span class="inlinecode"><span class="id" title="var">color</span></span>;

</li>
<li> if <span class="inlinecode"><span class="id" title="var">p</span></span> is a constructor expression belonging to the set <span class="inlinecode"><span class="id" title="var">rgb</span></span>,
      then <span class="inlinecode"><span class="id" title="var">primary</span></span> <span class="inlinecode"><span class="id" title="var">p</span></span> (pronounced "the constructor <span class="inlinecode"><span class="id" title="var">primary</span></span> applied
      to the argument <span class="inlinecode"><span class="id" title="var">p</span></span>") is a constructor expression belonging to
      the set <span class="inlinecode"><span class="id" title="var">color</span></span>; and

</li>
<li> constructor expressions formed in these ways are the <i>only</i> ones
      belonging to the sets <span class="inlinecode"><span class="id" title="var">rgb</span></span> and <span class="inlinecode"><span class="id" title="var">color</span></span>. 
</li>
</ul>

<div class="paragraph"> </div>

 We can define functions on colors using pattern matching just as
    we did for <span class="inlinecode"><span class="id" title="var">day</span></span> and <span class="inlinecode"><span class="id" title="var">bool</span></span>. 
</div>
<div class="code">

<span class="id" title="keyword">Definition</span> <a id="monochrome" class="idref" href="#monochrome"><span class="id" title="definition">monochrome</span></a> (<a id="c:30" class="idref" href="#c:30"><span class="id" title="binder">c</span></a> : <a class="idref" href="Basics.html#color"><span class="id" title="inductive">color</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#c:30"><span class="id" title="variable">c</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#black"><span class="id" title="constructor">black</span></a> ⇒ <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#white"><span class="id" title="constructor">white</span></a> ⇒ <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#primary"><span class="id" title="constructor">primary</span></a> <span class="id" title="var">p</span> ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/>
</div>

<div class="doc">
Since the <span class="inlinecode"><span class="id" title="var">primary</span></span> constructor takes an argument, a pattern
    matching <span class="inlinecode"><span class="id" title="var">primary</span></span> should include either a variable (as above --
    note that we can choose its name freely) or a constant of
    appropriate type (as below). 
</div>
<div class="code">

<span class="id" title="keyword">Definition</span> <a id="isred" class="idref" href="#isred"><span class="id" title="definition">isred</span></a> (<a id="c:32" class="idref" href="#c:32"><span class="id" title="binder">c</span></a> : <a class="idref" href="Basics.html#color"><span class="id" title="inductive">color</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#c:32"><span class="id" title="variable">c</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#black"><span class="id" title="constructor">black</span></a> ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#white"><span class="id" title="constructor">white</span></a> ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#primary"><span class="id" title="constructor">primary</span></a> <a class="idref" href="Basics.html#red"><span class="id" title="constructor">red</span></a> ⇒ <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#primary"><span class="id" title="constructor">primary</span></a> <span class="id" title="var">_</span> ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/>
</div>

<div class="doc">
The pattern "<span class="inlinecode"><span class="id" title="var">primary</span></span> <span class="inlinecode"><span class="id" title="var">_</span></span>" here is shorthand for "the constructor
    <span class="inlinecode"><span class="id" title="var">primary</span></span> applied to any <span class="inlinecode"><span class="id" title="var">rgb</span></span> constructor except <span class="inlinecode"><span class="id" title="tactic">red</span></span>."  (The
    wildcard pattern <span class="inlinecode"><span class="id" title="var">_</span></span> has the same effect as the dummy pattern
    variable <span class="inlinecode"><span class="id" title="var">p</span></span> in the definition of <span class="inlinecode"><span class="id" title="var">monochrome</span></span>.) 
</div>

<div class="doc">
<a id="lab29"></a><h2 class="section">Modules</h2>

<div class="paragraph"> </div>

 Coq provides a <i>module system</i> to aid in organizing large
    developments.  We won't need most of its features,
    but one is useful: If we enclose a collection of declarations
    between <span class="inlinecode"><span class="id" title="keyword">Module</span></span> <span class="inlinecode"><span class="id" title="var">X</span></span> and <span class="inlinecode"><span class="id" title="keyword">End</span></span> <span class="inlinecode"><span class="id" title="var">X</span></span> markers, then, in the remainder of
    the file after the <span class="inlinecode"><span class="id" title="keyword">End</span></span>, these definitions are referred to by
    names like <span class="inlinecode"><span class="id" title="var">X.foo</span></span> instead of just <span class="inlinecode"><span class="id" title="var">foo</span></span>.  We will use this
    feature to limit the scope of definitions, so that we are free to
    reuse names. 
</div>
<div class="code">

<span class="id" title="keyword">Module</span> <a id="Playground" class="idref" href="#Playground"><span class="id" title="module">Playground</span></a>.<br/>
&nbsp;&nbsp;<span class="id" title="keyword">Definition</span> <a id="Playground.b" class="idref" href="#Playground.b"><span class="id" title="definition">b</span></a> : <a class="idref" href="Basics.html#rgb"><span class="id" title="inductive">rgb</span></a> := <a class="idref" href="Basics.html#blue"><span class="id" title="constructor">blue</span></a>.<br/>
<span class="id" title="keyword">End</span> <a class="idref" href="Basics.html#Playground"><span class="id" title="module">Playground</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Definition</span> <a id="b" class="idref" href="#b"><span class="id" title="definition">b</span></a> : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> := <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Check</span> <a class="idref" href="Basics.html#b"><span class="id" title="definition">Playground.b</span></a> : <a class="idref" href="Basics.html#rgb"><span class="id" title="inductive">rgb</span></a>.<br/>
<span class="id" title="keyword">Check</span> <a class="idref" href="Basics.html#b"><span class="id" title="definition">b</span></a> : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>.<br/>
</div>

<div class="doc">
<a id="lab30"></a><h2 class="section">Tuples</h2>

</div>
<div class="code">

<span class="id" title="keyword">Module</span> <a id="TuplePlayground" class="idref" href="#TuplePlayground"><span class="id" title="module">TuplePlayground</span></a>.<br/>
</div>

<div class="doc">
A single constructor with multiple parameters can be used
    to create a tuple type. As an example, consider representing
    the four bits in a nybble (half a byte). We first define
    a datatype <span class="inlinecode"><span class="id" title="var">bit</span></span> that resembles <span class="inlinecode"><span class="id" title="var">bool</span></span> (using the
    constructors <span class="inlinecode"><span class="id" title="var">B<sub>0</sub></span></span> and <span class="inlinecode"><span class="id" title="var">B<sub>1</sub></span></span> for the two possible bit values)
    and then define the datatype <span class="inlinecode"><span class="id" title="var">nybble</span></span>, which is essentially
    a tuple of four bits. 
</div>
<div class="code">

<span class="id" title="keyword">Inductive</span> <a id="TuplePlayground.bit" class="idref" href="#TuplePlayground.bit"><span class="id" title="inductive">bit</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
&nbsp;&nbsp;| <a id="TuplePlayground.B0" class="idref" href="#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a><br/>
&nbsp;&nbsp;| <a id="TuplePlayground.B1" class="idref" href="#TuplePlayground.B1"><span class="id" title="constructor">B<sub>1</sub></span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Inductive</span> <a id="TuplePlayground.nybble" class="idref" href="#TuplePlayground.nybble"><span class="id" title="inductive">nybble</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
&nbsp;&nbsp;| <a id="TuplePlayground.bits" class="idref" href="#TuplePlayground.bits"><span class="id" title="constructor">bits</span></a> (<a id="b<sub>0</sub>:38" class="idref" href="#b<sub>0</sub>:38"><span class="id" title="binder">b<sub>0</sub></span></a> <a id="b<sub>1</sub>:39" class="idref" href="#b<sub>1</sub>:39"><span class="id" title="binder">b<sub>1</sub></span></a> <a id="b<sub>2</sub>:40" class="idref" href="#b<sub>2</sub>:40"><span class="id" title="binder">b<sub>2</sub></span></a> <a id="b<sub>3</sub>:41" class="idref" href="#b<sub>3</sub>:41"><span class="id" title="binder">b<sub>3</sub></span></a> : <a class="idref" href="Basics.html#TuplePlayground.bit"><span class="id" title="inductive">bit</span></a>).<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Check</span> (<a class="idref" href="Basics.html#TuplePlayground.bits"><span class="id" title="constructor">bits</span></a> <a class="idref" href="Basics.html#TuplePlayground.B1"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B1"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a>)<br/>
&nbsp;&nbsp;: <a class="idref" href="Basics.html#TuplePlayground.nybble"><span class="id" title="inductive">nybble</span></a>.<br/>
</div>

<div class="doc">
The <span class="inlinecode"><span class="id" title="var">bits</span></span> constructor acts as a wrapper for its contents.
    Unwrapping can be done by pattern-matching, as in the <span class="inlinecode"><span class="id" title="var">all_zero</span></span>
    function which tests a nybble to see if all its bits are <span class="inlinecode"><span class="id" title="var">B<sub>0</sub></span></span>.  We
    use underscore (_) as a <i>wildcard pattern</i> to avoid inventing
    variable names that will not be used. 
</div>
<div class="code">

<span class="id" title="keyword">Definition</span> <a id="TuplePlayground.all_zero" class="idref" href="#TuplePlayground.all_zero"><span class="id" title="definition">all_zero</span></a> (<a id="nb:42" class="idref" href="#nb:42"><span class="id" title="binder">nb</span></a> : <a class="idref" href="Basics.html#TuplePlayground.nybble"><span class="id" title="inductive">nybble</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#nb:42"><span class="id" title="variable">nb</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| (<a class="idref" href="Basics.html#TuplePlayground.bits"><span class="id" title="constructor">bits</span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a>) ⇒ <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
&nbsp;&nbsp;| (<a class="idref" href="Basics.html#TuplePlayground.bits"><span class="id" title="constructor">bits</span></a> <span class="id" title="var">_</span> <span class="id" title="var">_</span> <span class="id" title="var">_</span> <span class="id" title="var">_</span>) ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Compute</span> (<a class="idref" href="Basics.html#TuplePlayground.all_zero"><span class="id" title="definition">all_zero</span></a> (<a class="idref" href="Basics.html#TuplePlayground.bits"><span class="id" title="constructor">bits</span></a> <a class="idref" href="Basics.html#TuplePlayground.B1"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B1"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a>)).<br/>
<span class="comment">(*&nbsp;===&gt;&nbsp;false&nbsp;:&nbsp;bool&nbsp;*)</span><br/>
<span class="id" title="keyword">Compute</span> (<a class="idref" href="Basics.html#TuplePlayground.all_zero"><span class="id" title="definition">all_zero</span></a> (<a class="idref" href="Basics.html#TuplePlayground.bits"><span class="id" title="constructor">bits</span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a> <a class="idref" href="Basics.html#TuplePlayground.B0"><span class="id" title="constructor">B<sub>0</sub></span></a>)).<br/>
<span class="comment">(*&nbsp;===&gt;&nbsp;true&nbsp;:&nbsp;bool&nbsp;*)</span><br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">End</span> <a class="idref" href="Basics.html#TuplePlayground"><span class="id" title="module">TuplePlayground</span></a>.<br/>
</div>

<div class="doc">
<a id="lab31"></a><h2 class="section">Numbers</h2>

<div class="paragraph"> </div>

 We put this section in a module so that our own definition of
    natural numbers does not interfere with the one from the
    standard library.  In the rest of the book, we'll want to use
    the standard library's. 
</div>
<div class="code">

<span class="id" title="keyword">Module</span> <a id="NatPlayground" class="idref" href="#NatPlayground"><span class="id" title="module">NatPlayground</span></a>.<br/>
</div>

<div class="doc">
All the types we have defined so far -- both "enumerated
    types" such as <span class="inlinecode"><span class="id" title="var">day</span></span>, <span class="inlinecode"><span class="id" title="var">bool</span></span>, and <span class="inlinecode"><span class="id" title="var">bit</span></span> and tuple types such as
    <span class="inlinecode"><span class="id" title="var">nybble</span></span> built from them -- are finite.  The natural numbers, on
    the other hand, are an infinite set, so we'll need to use a
    slightly richer form of type declaration to represent them.

<div class="paragraph"> </div>

    There are many representations of numbers to choose from. We are
    most familiar with decimal notation (base 10), using the digits 0
    through 9, for example, to form the number 123.  You may have
    encountered hexadecimal notation (base 16), in which the same
    number is represented as 7B, or octal (base 8), where it is 173,
    or binary (base 2), where it is 1111011. Using an enumerated type
    to represent digits, we could use any of these as our
    representation natural numbers. Indeed, there are circumstances
    where each of these choices would be useful.

<div class="paragraph"> </div>

    The binary representation is valuable in computer hardware because
    the digits can be represented with just two distinct voltage
    levels, resulting in simple circuitry. Analogously, we wish here
    to choose a representation that makes <i>proofs</i> simpler.

<div class="paragraph"> </div>

    In fact, there is a representation of numbers that is even simpler
    than binary, namely unary (base 1), in which only a single digit
    is used (as one might do to count days in prison by scratching on
    the walls). To represent unary numbers with a Coq datatype, we use
    two constructors. The capital-letter <span class="inlinecode"><span class="id" title="var">O</span></span> constructor represents
    zero.  When the <span class="inlinecode"><span class="id" title="var">S</span></span> constructor is applied to the representation
    of the natural number n, the result is the representation of
    n+1, where <span class="inlinecode"><span class="id" title="var">S</span></span> stands for "successor" (or "scratch" if one is in
    prison).  Here is the complete datatype definition. 
</div>
<div class="code">

<span class="id" title="keyword">Inductive</span> <a id="NatPlayground.nat" class="idref" href="#NatPlayground.nat"><span class="id" title="inductive">nat</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
&nbsp;&nbsp;| <a id="NatPlayground.O" class="idref" href="#NatPlayground.O"><span class="id" title="constructor">O</span></a><br/>
&nbsp;&nbsp;| <a id="NatPlayground.S" class="idref" href="#NatPlayground.S"><span class="id" title="constructor">S</span></a> (<a id="n:46" class="idref" href="#n:46"><span class="id" title="binder">n</span></a> : <a class="idref" href="Basics.html#nat:44"><span class="id" title="inductive">nat</span></a>).<br/>
</div>

<div class="doc">
With this definition, 0 is represented by <span class="inlinecode"><span class="id" title="var">O</span></span>, 1 by <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span></span>,
    2 by <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span>)</span>, and so on. 
<div class="paragraph"> </div>

 Informally, the clauses of the definition can be read:
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" title="var">O</span></span> is a natural number (remember this is the letter "<span class="inlinecode"><span class="id" title="var">O</span></span>,"
        not the numeral "<span class="inlinecode">0</span>").

</li>
<li> <span class="inlinecode"><span class="id" title="var">S</span></span> can be put in front of a natural number to yield another
        one -- if <span class="inlinecode"><span class="id" title="var">n</span></span> is a natural number, then <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">n</span></span> is too. 
</li>
</ul>

<div class="paragraph"> </div>

 Again, let's look at this in a little more detail.  The definition
    of <span class="inlinecode"><span class="id" title="var">nat</span></span> says how expressions in the set <span class="inlinecode"><span class="id" title="var">nat</span></span> can be built:

<div class="paragraph"> </div>

<ul class="doclist">
<li> the constructor expression <span class="inlinecode"><span class="id" title="var">O</span></span> belongs to the set <span class="inlinecode"><span class="id" title="var">nat</span></span>;

</li>
<li> if <span class="inlinecode"><span class="id" title="var">n</span></span> is a constructor expression belonging to the set <span class="inlinecode"><span class="id" title="var">nat</span></span>,
      then <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">n</span></span> is also a constructor expression belonging to the set
      <span class="inlinecode"><span class="id" title="var">nat</span></span>; and

</li>
<li> constructor expressions formed in these two ways are the only
      ones belonging to the set <span class="inlinecode"><span class="id" title="var">nat</span></span>. 
</li>
</ul>

<div class="paragraph"> </div>

 These conditions are the precise force of the <span class="inlinecode"><span class="id" title="keyword">Inductive</span></span>
    declaration.  They imply that the constructor expression <span class="inlinecode"><span class="id" title="var">O</span></span>, the
    constructor expression <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span></span>, the constructor expression <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span>
    <span class="inlinecode"><span class="id" title="var">O</span>)</span>, the constructor expression <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span>))</span>, and so on all
    belong to the set <span class="inlinecode"><span class="id" title="var">nat</span></span>, while other constructor expressions, like
    <span class="inlinecode"><span class="id" title="var">true</span></span>, <span class="inlinecode"><span class="id" title="var">andb</span></span> <span class="inlinecode"><span class="id" title="var">true</span></span> <span class="inlinecode"><span class="id" title="var">false</span></span>, <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">false</span>)</span>, and <span class="inlinecode"><span class="id" title="var">O</span></span> <span class="inlinecode">(<span class="id" title="var">O</span></span> <span class="inlinecode">(<span class="id" title="var">O</span></span> <span class="inlinecode"><span class="id" title="var">S</span>))</span> do
    not.

<div class="paragraph"> </div>

    A critical point here is that what we've done so far is just to
    define a <i>representation</i> of numbers: a way of writing them down.
    The names <span class="inlinecode"><span class="id" title="var">O</span></span> and <span class="inlinecode"><span class="id" title="var">S</span></span> are arbitrary, and at this point they have
    no special meaning -- they are just two different marks that we
    can use to write down numbers (together with a rule that says any
    <span class="inlinecode"><span class="id" title="var">nat</span></span> will be written as some string of <span class="inlinecode"><span class="id" title="var">S</span></span> marks followed by an
    <span class="inlinecode"><span class="id" title="var">O</span></span>).  If we like, we can write essentially the same definition
    this way: 
</div>
<div class="code">

<span class="id" title="keyword">Inductive</span> <a id="NatPlayground.nat'" class="idref" href="#NatPlayground.nat'"><span class="id" title="inductive">nat'</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
&nbsp;&nbsp;| <a id="NatPlayground.stop" class="idref" href="#NatPlayground.stop"><span class="id" title="constructor">stop</span></a><br/>
&nbsp;&nbsp;| <a id="NatPlayground.tick" class="idref" href="#NatPlayground.tick"><span class="id" title="constructor">tick</span></a> (<a id="foo:49" class="idref" href="#foo:49"><span class="id" title="binder">foo</span></a> : <a class="idref" href="Basics.html#nat':47"><span class="id" title="inductive">nat'</span></a>).<br/>
</div>

<div class="doc">
The <i>interpretation</i> of these marks comes from how we use them to
    compute. 
<div class="paragraph"> </div>

 We can do this by writing functions that pattern match on
    representations of natural numbers just as we did above with
    booleans and days -- for example, here is the predecessor
    function: 
</div>
<div class="code">

<span class="id" title="keyword">Definition</span> <a id="NatPlayground.pred" class="idref" href="#NatPlayground.pred"><span class="id" title="definition">pred</span></a> (<a id="n:50" class="idref" href="#n:50"><span class="id" title="binder">n</span></a> : <a class="idref" href="Basics.html#NatPlayground.nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="Basics.html#NatPlayground.nat"><span class="id" title="inductive">nat</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#n:50"><span class="id" title="variable">n</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#NatPlayground.O"><span class="id" title="constructor">O</span></a> ⇒ <a class="idref" href="Basics.html#NatPlayground.O"><span class="id" title="constructor">O</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="Basics.html#NatPlayground.S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">n'</span> ⇒ <span class="id" title="var">n'</span><br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/>
</div>

<div class="doc">
The second branch can be read: "if <span class="inlinecode"><span class="id" title="var">n</span></span> has the form <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">n'</span></span>
    for some <span class="inlinecode"><span class="id" title="var">n'</span></span>, then return <span class="inlinecode"><span class="id" title="var">n'</span></span>."  
<div class="paragraph"> </div>

 The following <span class="inlinecode"><span class="id" title="keyword">End</span></span> command closes the current module, so
    <span class="inlinecode"><span class="id" title="var">nat</span></span> will refer back to the type from the standard library. 
</div>
<div class="code">

<span class="id" title="keyword">End</span> <a class="idref" href="Basics.html#NatPlayground"><span class="id" title="module">NatPlayground</span></a>.<br/>
</div>

<div class="doc">
Because natural numbers are such a pervasive form of data,
    Coq provides a tiny bit of built-in magic for parsing and printing
    them: ordinary decimal numerals can be used as an alternative to
    the "unary" notation defined by the constructors <span class="inlinecode"><span class="id" title="var">S</span></span> and <span class="inlinecode"><span class="id" title="var">O</span></span>.  Coq
    prints numbers in decimal form by default: 
</div>
<div class="code">

<span class="id" title="keyword">Check</span> (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a>)))).<br/>
<span class="comment">(*&nbsp;===&gt;&nbsp;4&nbsp;:&nbsp;nat&nbsp;*)</span><br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Definition</span> <a id="minustwo" class="idref" href="#minustwo"><span class="id" title="definition">minustwo</span></a> (<a id="n:52" class="idref" href="#n:52"><span class="id" title="binder">n</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#n:52"><span class="id" title="variable">n</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a> ⇒ <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a> ⇒ <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">n'</span>) ⇒ <span class="id" title="var">n'</span><br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Compute</span> (<a class="idref" href="Basics.html#minustwo"><span class="id" title="definition">minustwo</span></a> 4).<br/>
<span class="comment">(*&nbsp;===&gt;&nbsp;2&nbsp;:&nbsp;nat&nbsp;*)</span><br/>
</div>

<div class="doc">
The constructor <span class="inlinecode"><span class="id" title="var">S</span></span> has the type <span class="inlinecode"><span class="id" title="var">nat</span></span> <span class="inlinecode">→</span> <span class="inlinecode"><span class="id" title="var">nat</span></span>, just like functions
    such as <span class="inlinecode"><span class="id" title="var">pred</span></span> and <span class="inlinecode"><span class="id" title="var">minustwo</span></span>: 
</div>
<div class="code">

<span class="id" title="keyword">Check</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a>        : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'-&gt;'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>.<br/>
<span class="id" title="keyword">Check</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#pred"><span class="id" title="abbreviation">pred</span></a>     : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'-&gt;'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>.<br/>
<span class="id" title="keyword">Check</span> <a class="idref" href="Basics.html#minustwo"><span class="id" title="definition">minustwo</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'-&gt;'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>.<br/>
</div>

<div class="doc">
These are all things that can be applied to a number to yield a
    number.  However, there is a fundamental difference between <span class="inlinecode"><span class="id" title="var">S</span></span>
    and the other two: functions like <span class="inlinecode"><span class="id" title="var">pred</span></span> and <span class="inlinecode"><span class="id" title="var">minustwo</span></span> are
    defined by giving <i>computation rules</i> -- e.g., the definition of
    <span class="inlinecode"><span class="id" title="var">pred</span></span> says that <span class="inlinecode"><span class="id" title="var">pred</span></span> <span class="inlinecode">2</span> can be simplified to <span class="inlinecode">1</span> -- while the
    definition of <span class="inlinecode"><span class="id" title="var">S</span></span> has no such behavior attached.  Although it is
    <i>like</i> a function in the sense that it can be applied to an
    argument, it does not <i>do</i> anything at all!  It is just a way of
    writing down numbers.

<div class="paragraph"> </div>

    (Think about standard decimal numerals: the numeral <span class="inlinecode">1</span> is not a
    computation; it's a piece of data.  When we write <span class="inlinecode">111</span> to mean
    the number one hundred and eleven, we are using <span class="inlinecode">1</span>, three times,
    to write down a concrete representation of a number.)

<div class="paragraph"> </div>

    Now let's go on and define some more functions over numbers.

<div class="paragraph"> </div>

    For most interesting computations involving numbers, simple
    pattern matching is not enough: we also need recursion.  For
    example, to check that a number <span class="inlinecode"><span class="id" title="var">n</span></span> is even, we may need to
    recursively check whether <span class="inlinecode"><span class="id" title="var">n</span>-2</span> is even.  Such functions are
    introduced with the keyword <span class="inlinecode"><span class="id" title="keyword">Fixpoint</span></span> instead of <span class="inlinecode"><span class="id" title="keyword">Definition</span></span>. 
</div>
<div class="code">

<span class="id" title="keyword">Fixpoint</span> <a id="even" class="idref" href="#even"><span class="id" title="definition">even</span></a> (<a id="n:54" class="idref" href="#n:54"><span class="id" title="binder">n</span></a>:<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#n:54"><span class="id" title="variable">n</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a>        ⇒ <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a>      ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">n'</span>) ⇒ <a class="idref" href="Basics.html#even:55"><span class="id" title="definition">even</span></a> <span class="id" title="var">n'</span><br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/>
</div>

<div class="doc">
We could define <span class="inlinecode"><span class="id" title="var">odd</span></span> by a similar <span class="inlinecode"><span class="id" title="keyword">Fixpoint</span></span> declaration, but
    here is a simpler way: 
</div>
<div class="code">

<span class="id" title="keyword">Definition</span> <a id="odd" class="idref" href="#odd"><span class="id" title="definition">odd</span></a> (<a id="n:57" class="idref" href="#n:57"><span class="id" title="binder">n</span></a>:<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
&nbsp;&nbsp;<a class="idref" href="Basics.html#negb"><span class="id" title="definition">negb</span></a> (<a class="idref" href="Basics.html#even"><span class="id" title="definition">even</span></a> <a class="idref" href="Basics.html#n:57"><span class="id" title="variable">n</span></a>).<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="test_odd1" class="idref" href="#test_odd1"><span class="id" title="definition">test_odd1</span></a>:    <a class="idref" href="Basics.html#odd"><span class="id" title="definition">odd</span></a> 1 <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_odd2" class="idref" href="#test_odd2"><span class="id" title="definition">test_odd2</span></a>:    <a class="idref" href="Basics.html#odd"><span class="id" title="definition">odd</span></a> 4 <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
(You may notice if you step through these proofs that
    <span class="inlinecode"><span class="id" title="tactic">simpl</span></span> actually has no effect on the goal -- all of the work is
    done by <span class="inlinecode"><span class="id" title="tactic">reflexivity</span></span>.  We'll discuss why that is shortly.)

<div class="paragraph"> </div>

    Naturally, we can also define multi-argument functions by
    recursion.  
</div>
<div class="code">

<span class="id" title="keyword">Module</span> <a id="NatPlayground2" class="idref" href="#NatPlayground2"><span class="id" title="module">NatPlayground2</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Fixpoint</span> <a id="NatPlayground2.plus" class="idref" href="#NatPlayground2.plus"><span class="id" title="definition">plus</span></a> (<a id="n:58" class="idref" href="#n:58"><span class="id" title="binder">n</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) (<a id="m:59" class="idref" href="#m:59"><span class="id" title="binder">m</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#n:58"><span class="id" title="variable">n</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a> ⇒ <a class="idref" href="Basics.html#m:59"><span class="id" title="variable">m</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">n'</span> ⇒ <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> (<a class="idref" href="Basics.html#plus:60"><span class="id" title="definition">plus</span></a> <span class="id" title="var">n'</span> <a class="idref" href="Basics.html#m:59"><span class="id" title="variable">m</span></a>)<br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/>
</div>

<div class="doc">
Adding three to two now gives us five, as we'd expect. 
</div>
<div class="code">

<span class="id" title="keyword">Compute</span> (<a class="idref" href="Basics.html#NatPlayground2.plus"><span class="id" title="definition">plus</span></a> 3 2).<br/>
<span class="comment">(*&nbsp;===&gt;&nbsp;5&nbsp;:&nbsp;nat&nbsp;*)</span><br/>
</div>

<div class="doc">
The steps of simplification that Coq performs can be
    visualized as follows: 
</div>
<div class="code">

<span class="comment">(*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="inlinecode"><span class="id" title="var">plus</span></span> <span class="inlinecode">3</span> <span class="inlinecode">2</span><br/>
&nbsp;&nbsp;&nbsp;i.e.&nbsp;<span class="inlinecode"><span class="id" title="var">plus</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span>)))</span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span>))</span><br/>
&nbsp;&nbsp;&nbsp;&nbsp;==&gt;&nbsp;<span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">plus</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span>))</span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span>)))</span><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;by&nbsp;the&nbsp;second&nbsp;clause&nbsp;of&nbsp;the&nbsp;<span class="inlinecode"><span class="id" title="keyword">match</span></span><br/>
&nbsp;&nbsp;&nbsp;&nbsp;==&gt;&nbsp;<span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">plus</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span>)</span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span>))))</span><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;by&nbsp;the&nbsp;second&nbsp;clause&nbsp;of&nbsp;the&nbsp;<span class="inlinecode"><span class="id" title="keyword">match</span></span><br/>
&nbsp;&nbsp;&nbsp;&nbsp;==&gt;&nbsp;<span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">plus</span></span> <span class="inlinecode"><span class="id" title="var">O</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span>)))))</span><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;by&nbsp;the&nbsp;second&nbsp;clause&nbsp;of&nbsp;the&nbsp;<span class="inlinecode"><span class="id" title="keyword">match</span></span><br/>
&nbsp;&nbsp;&nbsp;&nbsp;==&gt;&nbsp;<span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span>))))</span><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;by&nbsp;the&nbsp;first&nbsp;clause&nbsp;of&nbsp;the&nbsp;<span class="inlinecode"><span class="id" title="keyword">match</span></span><br/>
&nbsp;&nbsp;&nbsp;i.e.&nbsp;<span class="inlinecode">5</span>&nbsp;&nbsp;*)</span><br/>
</div>

<div class="doc">
As a notational convenience, if two or more arguments have
    the same type, they can be written together.  In the following
    definition, <span class="inlinecode">(<span class="id" title="var">n</span></span> <span class="inlinecode"><span class="id" title="var">m</span></span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" title="var">nat</span>)</span> means just the same as if we had written
    <span class="inlinecode">(<span class="id" title="var">n</span></span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" title="var">nat</span>)</span> <span class="inlinecode">(<span class="id" title="var">m</span></span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" title="var">nat</span>)</span>. 
</div>
<div class="code">

<span class="id" title="keyword">Fixpoint</span> <a id="NatPlayground2.mult" class="idref" href="#NatPlayground2.mult"><span class="id" title="definition">mult</span></a> (<a id="n:62" class="idref" href="#n:62"><span class="id" title="binder">n</span></a> <a id="m:63" class="idref" href="#m:63"><span class="id" title="binder">m</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#n:62"><span class="id" title="variable">n</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a> ⇒ <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">n'</span> ⇒ <a class="idref" href="Basics.html#NatPlayground2.plus"><span class="id" title="definition">plus</span></a> <a class="idref" href="Basics.html#m:63"><span class="id" title="variable">m</span></a> (<a class="idref" href="Basics.html#mult:64"><span class="id" title="definition">mult</span></a> <span class="id" title="var">n'</span> <a class="idref" href="Basics.html#m:63"><span class="id" title="variable">m</span></a>)<br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="NatPlayground2.test_mult1" class="idref" href="#NatPlayground2.test_mult1"><span class="id" title="definition">test_mult1</span></a>: <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#NatPlayground2.mult"><span class="id" title="definition">mult</span></a> 3 3<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> 9.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
You can match two expressions at once by putting a comma
    between them: 
</div>
<div class="code">

<span class="id" title="keyword">Fixpoint</span> <a id="NatPlayground2.minus" class="idref" href="#NatPlayground2.minus"><span class="id" title="definition">minus</span></a> (<a id="n:66" class="idref" href="#n:66"><span class="id" title="binder">n</span></a> <a id="m:67" class="idref" href="#m:67"><span class="id" title="binder">m</span></a>:<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#n:66"><span class="id" title="variable">n</span></a>, <a class="idref" href="Basics.html#m:67"><span class="id" title="variable">m</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a>   , <span class="id" title="var">_</span>    ⇒ <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">_</span> , <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a>    ⇒ <a class="idref" href="Basics.html#n:66"><span class="id" title="variable">n</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">n'</span>, <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">m'</span> ⇒ <a class="idref" href="Basics.html#minus:68"><span class="id" title="definition">minus</span></a> <span class="id" title="var">n'</span> <span class="id" title="var">m'</span><br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">End</span> <a class="idref" href="Basics.html#NatPlayground2"><span class="id" title="module">NatPlayground2</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Fixpoint</span> <a id="exp" class="idref" href="#exp"><span class="id" title="definition">exp</span></a> (<a id="base:71" class="idref" href="#base:71"><span class="id" title="binder">base</span></a> <a id="power:72" class="idref" href="#power:72"><span class="id" title="binder">power</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#power:72"><span class="id" title="variable">power</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a> ⇒ <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">p</span> ⇒ <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#mult"><span class="id" title="abbreviation">mult</span></a> <a class="idref" href="Basics.html#base:71"><span class="id" title="variable">base</span></a> (<a class="idref" href="Basics.html#exp:73"><span class="id" title="definition">exp</span></a> <a class="idref" href="Basics.html#base:71"><span class="id" title="variable">base</span></a> <span class="id" title="var">p</span>)<br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/>
</div>

<div class="doc">
<a id="lab32"></a><h4 class="section">Exercise: 1 star, standard (factorial)</h4>
 Recall the standard mathematical factorial function:
<pre>
       factorial(0)  =  1
       factorial(n)  =  n * factorial(n-1)     (if n&gt;0)
</pre>
    Translate this into Coq. 
</div>
<div class="code">

<span class="id" title="keyword">Fixpoint</span> <a id="factorial" class="idref" href="#factorial"><span class="id" title="definition">factorial</span></a> (<a id="n:75" class="idref" href="#n:75"><span class="id" title="binder">n</span></a>:<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a><br/>
&nbsp;&nbsp;<span class="comment">(*&nbsp;REPLACE&nbsp;THIS&nbsp;LINE&nbsp;WITH&nbsp;":=&nbsp;_your_definition_&nbsp;."&nbsp;*)</span>. <span class="id" title="var">Admitted</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="test_factorial1" class="idref" href="#test_factorial1"><span class="id" title="definition">test_factorial1</span></a>:          <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#factorial"><span class="id" title="axiom">factorial</span></a> 3<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> 6.<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_factorial2" class="idref" href="#test_factorial2"><span class="id" title="definition">test_factorial2</span></a>:          <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#factorial"><span class="id" title="axiom">factorial</span></a> 5<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#mult"><span class="id" title="abbreviation">mult</span></a> 10 12<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a>.<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>&#9744;</font>
</div>

<div class="doc"> 
<div class="paragraph"> </div>

 Again, we can make numerical expressions easier to read and write
    by introducing notations for addition, multiplication, and
    subtraction. 
</div>
<div class="code">

<span class="id" title="keyword">Notation</span> <a id="0dacc1786c5ba797d47dd85006231633" class="idref" href="#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">&quot;</span></a>x + y" := (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#plus"><span class="id" title="abbreviation">plus</span></a> <span class="id" title="var">x</span> <span class="id" title="var">y</span>)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span class="id" title="tactic">at</span> <span class="id" title="keyword">level</span> 50, <span class="id" title="tactic">left</span> <span class="id" title="keyword">associativity</span>)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span class="id" title="var">nat_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="::nat_scope:x_'-'_x" class="idref" href="#::nat_scope:x_'-'_x"><span class="id" title="notation">&quot;</span></a>x - y" := (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#minus"><span class="id" title="abbreviation">minus</span></a> <span class="id" title="var">x</span> <span class="id" title="var">y</span>)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span class="id" title="tactic">at</span> <span class="id" title="keyword">level</span> 50, <span class="id" title="tactic">left</span> <span class="id" title="keyword">associativity</span>)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span class="id" title="var">nat_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="ea2ff3d561159081cea6fb2e8113cc<sub>54</sub>" class="idref" href="#ea2ff3d561159081cea6fb2e8113cc<sub>54</sub>"><span class="id" title="notation">&quot;</span></a>x * y" := (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#mult"><span class="id" title="abbreviation">mult</span></a> <span class="id" title="var">x</span> <span class="id" title="var">y</span>)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span class="id" title="tactic">at</span> <span class="id" title="keyword">level</span> 40, <span class="id" title="tactic">left</span> <span class="id" title="keyword">associativity</span>)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span class="id" title="var">nat_scope</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Check</span> (<a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">(</span></a>0 <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> 1<a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">)</span></a> <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> 1) : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>.<br/>
</div>

<div class="doc">
(The <span class="inlinecode"><span class="id" title="keyword">level</span></span>, <span class="inlinecode"><span class="id" title="keyword">associativity</span></span>, and <span class="inlinecode"><span class="id" title="var">nat_scope</span></span> annotations
    control how these notations are treated by Coq's parser.  The
    details are not important for present purposes, but interested
    readers can refer to the "More on Notation" section at the end of
    this chapter.)

<div class="paragraph"> </div>

    Note that these declarations do not change the definitions we've
    already made: they are simply instructions to the Coq parser to
    accept <span class="inlinecode"><span class="id" title="var">x</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" title="var">y</span></span> in place of <span class="inlinecode"><span class="id" title="var">plus</span></span> <span class="inlinecode"><span class="id" title="var">x</span></span> <span class="inlinecode"><span class="id" title="var">y</span></span> and, conversely, to the Coq
    pretty-printer to display <span class="inlinecode"><span class="id" title="var">plus</span></span> <span class="inlinecode"><span class="id" title="var">x</span></span> <span class="inlinecode"><span class="id" title="var">y</span></span> as <span class="inlinecode"><span class="id" title="var">x</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" title="var">y</span></span>. 
<div class="paragraph"> </div>

 When we say that Coq comes with almost nothing built-in, we really
    mean it: even equality testing is a user-defined operation!
    Here is a function <span class="inlinecode"><span class="id" title="var">eqb</span></span>, which tests natural numbers for
    <span class="inlinecode"><span class="id" title="var">eq</span></span>uality, yielding a <span class="inlinecode"><span class="id" title="var">b</span></span>oolean.  Note the use of nested
    <span class="inlinecode"><span class="id" title="keyword">match</span></span>es (we could also have used a simultaneous match, as we did
    in <span class="inlinecode"><span class="id" title="var">minus</span></span>.) 
</div>
<div class="code">

<span class="id" title="keyword">Fixpoint</span> <a id="eqb" class="idref" href="#eqb"><span class="id" title="definition">eqb</span></a> (<a id="n:77" class="idref" href="#n:77"><span class="id" title="binder">n</span></a> <a id="m:78" class="idref" href="#m:78"><span class="id" title="binder">m</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#n:77"><span class="id" title="variable">n</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a> ⇒ <span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#m:78"><span class="id" title="variable">m</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a> ⇒ <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">m'</span> ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="id" title="keyword">end</span><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">n'</span> ⇒ <span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#m:78"><span class="id" title="variable">m</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a> ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">m'</span> ⇒ <a class="idref" href="Basics.html#eqb:79"><span class="id" title="definition">eqb</span></a> <span class="id" title="var">n'</span> <span class="id" title="var">m'</span><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="id" title="keyword">end</span><br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/>
</div>

<div class="doc">
Similarly, the <span class="inlinecode"><span class="id" title="var">leb</span></span> function tests whether its first argument is
    less than or equal to its second argument, yielding a boolean. 
</div>
<div class="code">

<span class="id" title="keyword">Fixpoint</span> <a id="leb" class="idref" href="#leb"><span class="id" title="definition">leb</span></a> (<a id="n:83" class="idref" href="#n:83"><span class="id" title="binder">n</span></a> <a id="m:84" class="idref" href="#m:84"><span class="id" title="binder">m</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#n:83"><span class="id" title="variable">n</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a> ⇒ <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">n'</span> ⇒<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#m:84"><span class="id" title="variable">m</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a> ⇒ <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">m'</span> ⇒ <a class="idref" href="Basics.html#leb:85"><span class="id" title="definition">leb</span></a> <span class="id" title="var">n'</span> <span class="id" title="var">m'</span><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="id" title="keyword">end</span><br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="test_leb1" class="idref" href="#test_leb1"><span class="id" title="definition">test_leb1</span></a>:                <a class="idref" href="Basics.html#leb"><span class="id" title="definition">leb</span></a> 2 2 <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_leb2" class="idref" href="#test_leb2"><span class="id" title="definition">test_leb2</span></a>:                <a class="idref" href="Basics.html#leb"><span class="id" title="definition">leb</span></a> 2 4 <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_leb3" class="idref" href="#test_leb3"><span class="id" title="definition">test_leb3</span></a>:                <a class="idref" href="Basics.html#leb"><span class="id" title="definition">leb</span></a> 4 2 <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
We'll be using these (especially <span class="inlinecode"><span class="id" title="var">eqb</span></span>) a lot, so let's give
    them infix notations. 
</div>
<div class="code">

<span class="id" title="keyword">Notation</span> <a id="ad2ec4e405f68c46c0a176e3e94ae2e<sub>3</sub>" class="idref" href="#ad2ec4e405f68c46c0a176e3e94ae2e<sub>3</sub>"><span class="id" title="notation">&quot;</span></a>x =? y" := (<a class="idref" href="Basics.html#eqb"><span class="id" title="definition">eqb</span></a> <span class="id" title="var">x</span> <span class="id" title="var">y</span>) (<span class="id" title="tactic">at</span> <span class="id" title="keyword">level</span> 70) : <span class="id" title="var">nat_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="0f31f5c1c6b6a21a3a187247222bc9e<sub>4</sub>" class="idref" href="#0f31f5c1c6b6a21a3a187247222bc9e<sub>4</sub>"><span class="id" title="notation">&quot;</span></a>x &lt;=? y" := (<a class="idref" href="Basics.html#leb"><span class="id" title="definition">leb</span></a> <span class="id" title="var">x</span> <span class="id" title="var">y</span>) (<span class="id" title="tactic">at</span> <span class="id" title="keyword">level</span> 70) : <span class="id" title="var">nat_scope</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="test_leb3'" class="idref" href="#test_leb3'"><span class="id" title="definition">test_leb3'</span></a>: <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a>4 <a class="idref" href="Basics.html#0f31f5c1c6b6a21a3a187247222bc9e<sub>4</sub>"><span class="id" title="notation">&lt;=?</span></a> 2<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
We now have two symbols that look like equality: <span class="inlinecode">=</span> and
    <span class="inlinecode">=?</span>.  We'll have much more to say about the differences and
    similarities between them later. For now, the main thing to notice
    is that <span class="inlinecode"><span class="id" title="var">x</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" title="var">y</span></span> is a logical <i>claim</i> -- a "proposition" -- that we
    can try to prove, while <span class="inlinecode"><span class="id" title="var">x</span></span> <span class="inlinecode">=?</span> <span class="inlinecode"><span class="id" title="var">y</span></span> is an <i>expression</i> whose
    value (either <span class="inlinecode"><span class="id" title="var">true</span></span> or <span class="inlinecode"><span class="id" title="var">false</span></span>) we can compute. 
<div class="paragraph"> </div>

<a id="lab33"></a><h4 class="section">Exercise: 1 star, standard (ltb)</h4>
 The <span class="inlinecode"><span class="id" title="var">ltb</span></span> function tests natural numbers for <span class="inlinecode"><span class="id" title="var">l</span></span>ess-<span class="inlinecode"><span class="id" title="var">t</span></span>han,
    yielding a <span class="inlinecode"><span class="id" title="var">b</span></span>oolean.  Instead of making up a new <span class="inlinecode"><span class="id" title="keyword">Fixpoint</span></span> for
    this one, define it in terms of a previously defined
    function.  (It can be done with just one previously defined
    function, but you can use two if you want.) 
</div>
<div class="code">

<span class="id" title="keyword">Definition</span> <a id="ltb" class="idref" href="#ltb"><span class="id" title="definition">ltb</span></a> (<a id="n:88" class="idref" href="#n:88"><span class="id" title="binder">n</span></a> <a id="m:89" class="idref" href="#m:89"><span class="id" title="binder">m</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a><br/>
&nbsp;&nbsp;<span class="comment">(*&nbsp;REPLACE&nbsp;THIS&nbsp;LINE&nbsp;WITH&nbsp;":=&nbsp;_your_definition_&nbsp;."&nbsp;*)</span>. <span class="id" title="var">Admitted</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Notation</span> <a id="6b7621b45fff0af5e2b2cbb2bc2d4e1d" class="idref" href="#6b7621b45fff0af5e2b2cbb2bc2d4e1d"><span class="id" title="notation">&quot;</span></a>x &lt;? y" := (<a class="idref" href="Basics.html#ltb"><span class="id" title="axiom">ltb</span></a> <span class="id" title="var">x</span> <span class="id" title="var">y</span>) (<span class="id" title="tactic">at</span> <span class="id" title="keyword">level</span> 70) : <span class="id" title="var">nat_scope</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="test_ltb1" class="idref" href="#test_ltb1"><span class="id" title="definition">test_ltb1</span></a>:             <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#ltb"><span class="id" title="axiom">ltb</span></a> 2 2<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_ltb2" class="idref" href="#test_ltb2"><span class="id" title="definition">test_ltb2</span></a>:             <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#ltb"><span class="id" title="axiom">ltb</span></a> 2 4<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<span class="id" title="keyword">Example</span> <a id="test_ltb3" class="idref" href="#test_ltb3"><span class="id" title="definition">test_ltb3</span></a>:             <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#ltb"><span class="id" title="axiom">ltb</span></a> 4 2<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>&#9744;</font>
</div>


<div class="doc">
<a id="lab34"></a><h1 class="section">Proof by Simplification</h1>

<div class="paragraph"> </div>

 Now that we've defined a few datatypes and functions, let's
    turn to stating and proving properties of their behavior.
    Actually, we've already started doing this: each <span class="inlinecode"><span class="id" title="keyword">Example</span></span> in the
    previous sections makes a precise claim about the behavior of some
    function on some particular inputs.  The proofs of these claims
    were always the same: use <span class="inlinecode"><span class="id" title="tactic">simpl</span></span> to simplify both sides of the
    equation, then use <span class="inlinecode"><span class="id" title="tactic">reflexivity</span></span> to check that both sides contain
    identical values.

<div class="paragraph"> </div>

    The same sort of "proof by simplification" can be used to prove
    more interesting properties as well.  For example, the fact that
    <span class="inlinecode">0</span> is a "neutral element" for <span class="inlinecode">+</span> on the left can be proved just
    by observing that <span class="inlinecode">0</span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" title="var">n</span></span> reduces to <span class="inlinecode"><span class="id" title="var">n</span></span> no matter what <span class="inlinecode"><span class="id" title="var">n</span></span> is -- a
    fact that can be read directly off the definition of <span class="inlinecode"><span class="id" title="var">plus</span></span>. 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="plus_O_n" class="idref" href="#plus_O_n"><span class="id" title="lemma">plus_O_n</span></a> : <span class="id" title="keyword">∀</span> <a id="n:90" class="idref" href="#n:90"><span class="id" title="binder">n</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>, 0 <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> <a class="idref" href="Basics.html#n:90"><span class="id" title="variable">n</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#n:90"><span class="id" title="variable">n</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">intros</span> <span class="id" title="var">n</span>. <span class="id" title="tactic">simpl</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
(You may notice that the above statement looks different in
    the <span class="inlinecode">.<span class="id" title="var">v</span></span> file in your IDE than it does in the HTML rendition in
    your browser. In <span class="inlinecode">.<span class="id" title="var">v</span></span> files, we write the universal quantifier
    <span class="inlinecode"><span class="id" title="keyword">∀</span></span> using the reserved identifier "forall."  When the <span class="inlinecode">.<span class="id" title="var">v</span></span>
    files are converted to HTML, this gets transformed into the
    standard upside-down-A symbol.)

<div class="paragraph"> </div>

    This is a good place to mention that <span class="inlinecode"><span class="id" title="tactic">reflexivity</span></span> is a bit more
    powerful than we have acknowledged. In the examples we have seen,
    the calls to <span class="inlinecode"><span class="id" title="tactic">simpl</span></span> were actually not needed, because
    <span class="inlinecode"><span class="id" title="tactic">reflexivity</span></span> can perform some simplification automatically when
    checking that two sides are equal; <span class="inlinecode"><span class="id" title="tactic">simpl</span></span> was just added so that
    we could see the intermediate state -- after simplification but
    before finishing the proof.  Here is a shorter proof of the
    theorem: 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="plus_O_n'" class="idref" href="#plus_O_n'"><span class="id" title="lemma">plus_O_n'</span></a> : <span class="id" title="keyword">∀</span> <a id="n:91" class="idref" href="#n:91"><span class="id" title="binder">n</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>, 0 <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> <a class="idref" href="Basics.html#n:91"><span class="id" title="variable">n</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#n:91"><span class="id" title="variable">n</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">intros</span> <span class="id" title="var">n</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
Moreover, it will be useful to know that <span class="inlinecode"><span class="id" title="tactic">reflexivity</span></span> does
    somewhat <i>more</i> simplification than <span class="inlinecode"><span class="id" title="tactic">simpl</span></span> does -- for example,
    it tries "unfolding" defined terms, replacing them with their
    right-hand sides.  The reason for this difference is that, if
    reflexivity succeeds, the whole goal is finished and we don't need
    to look at whatever expanded expressions <span class="inlinecode"><span class="id" title="tactic">reflexivity</span></span> has created
    by all this simplification and unfolding; by contrast, <span class="inlinecode"><span class="id" title="tactic">simpl</span></span> is
    used in situations where we may have to read and understand the
    new goal that it creates, so we would not want it blindly
    expanding definitions and leaving the goal in a messy state.

<div class="paragraph"> </div>

    The form of the theorem we just stated and its proof are almost
    exactly the same as the simpler examples we saw earlier; there are
    just a few differences.

<div class="paragraph"> </div>

    First, we've used the keyword <span class="inlinecode"><span class="id" title="keyword">Theorem</span></span> instead of <span class="inlinecode"><span class="id" title="keyword">Example</span></span>.
    This difference is mostly a matter of style; the keywords
    <span class="inlinecode"><span class="id" title="keyword">Example</span></span> and <span class="inlinecode"><span class="id" title="keyword">Theorem</span></span> (and a few others, including <span class="inlinecode"><span class="id" title="keyword">Lemma</span></span>,
    <span class="inlinecode"><span class="id" title="keyword">Fact</span></span>, and <span class="inlinecode"><span class="id" title="keyword">Remark</span></span>) mean pretty much the same thing to Coq.

<div class="paragraph"> </div>

    Second, we've added the quantifier <span class="inlinecode"><span class="id" title="keyword">∀</span></span> <span class="inlinecode"><span class="id" title="var">n</span>:<span class="id" title="var">nat</span></span>, so that our
    theorem talks about <i>all</i> natural numbers <span class="inlinecode"><span class="id" title="var">n</span></span>.  Informally, to
    prove theorems of this form, we generally start by saying "Suppose
    <span class="inlinecode"><span class="id" title="var">n</span></span> is some number..."  Formally, this is achieved in the proof by
    <span class="inlinecode"><span class="id" title="tactic">intros</span></span> <span class="inlinecode"><span class="id" title="var">n</span></span>, which moves <span class="inlinecode"><span class="id" title="var">n</span></span> from the quantifier in the goal to a
    <i>context</i> of current assumptions. Note that we could have used
    another identifier instead of <span class="inlinecode"><span class="id" title="var">n</span></span> in the <span class="inlinecode"><span class="id" title="tactic">intros</span></span> clause, (though
    of course this might be confusing to human readers of the proof): 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="plus_O_n''" class="idref" href="#plus_O_n''"><span class="id" title="lemma">plus_O_n''</span></a> : <span class="id" title="keyword">∀</span> <a id="n:92" class="idref" href="#n:92"><span class="id" title="binder">n</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>, 0 <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> <a class="idref" href="Basics.html#n:92"><span class="id" title="variable">n</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#n:92"><span class="id" title="variable">n</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">intros</span> <span class="id" title="var">m</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
The keywords <span class="inlinecode"><span class="id" title="tactic">intros</span></span>, <span class="inlinecode"><span class="id" title="tactic">simpl</span></span>, and <span class="inlinecode"><span class="id" title="tactic">reflexivity</span></span> are examples of
    <i>tactics</i>.  A tactic is a command that is used between <span class="inlinecode"><span class="id" title="keyword">Proof</span></span> and
    <span class="inlinecode"><span class="id" title="keyword">Qed</span></span> to guide the process of checking some claim we are making.
    We will see several more tactics in the rest of this chapter and
    many more in future chapters. 
<div class="paragraph"> </div>

 Other similar theorems can be proved with the same pattern. 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="plus_1_l" class="idref" href="#plus_1_l"><span class="id" title="lemma">plus_1_l</span></a> : <span class="id" title="keyword">∀</span> <a id="n:93" class="idref" href="#n:93"><span class="id" title="binder">n</span></a>:<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>, 1 <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> <a class="idref" href="Basics.html#n:93"><span class="id" title="variable">n</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <a class="idref" href="Basics.html#n:93"><span class="id" title="variable">n</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">intros</span> <span class="id" title="var">n</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Theorem</span> <a id="mult_0_l" class="idref" href="#mult_0_l"><span class="id" title="lemma">mult_0_l</span></a> : <span class="id" title="keyword">∀</span> <a id="n:94" class="idref" href="#n:94"><span class="id" title="binder">n</span></a>:<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>, 0 <a class="idref" href="Basics.html#ea2ff3d561159081cea6fb2e8113cc<sub>54</sub>"><span class="id" title="notation">×</span></a> <a class="idref" href="Basics.html#n:94"><span class="id" title="variable">n</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> 0.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">intros</span> <span class="id" title="var">n</span>. <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
The <span class="inlinecode"><span class="id" title="var">_l</span></span> suffix in the names of these theorems is
    pronounced "on the left." 
<div class="paragraph"> </div>

 It is worth stepping through these proofs to observe how the
    context and the goal change.  You may want to add calls to <span class="inlinecode"><span class="id" title="tactic">simpl</span></span>
    before <span class="inlinecode"><span class="id" title="tactic">reflexivity</span></span> to see the simplifications that Coq performs
    on the terms before checking that they are equal. 
</div>

<div class="doc">
<a id="lab35"></a><h1 class="section">Proof by Rewriting</h1>

<div class="paragraph"> </div>

 The following theorem is a bit more interesting than the
    ones we've seen: 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="plus_id_example" class="idref" href="#plus_id_example"><span class="id" title="lemma">plus_id_example</span></a> : <span class="id" title="keyword">∀</span> <a id="n:95" class="idref" href="#n:95"><span class="id" title="binder">n</span></a> <a id="m:96" class="idref" href="#m:96"><span class="id" title="binder">m</span></a>:<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>,<br/>
&nbsp;&nbsp;<a class="idref" href="Basics.html#n:95"><span class="id" title="variable">n</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#m:96"><span class="id" title="variable">m</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'-&gt;'_x"><span class="id" title="notation">→</span></a><br/>
&nbsp;&nbsp;<a class="idref" href="Basics.html#n:95"><span class="id" title="variable">n</span></a> <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> <a class="idref" href="Basics.html#n:95"><span class="id" title="variable">n</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#m:96"><span class="id" title="variable">m</span></a> <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> <a class="idref" href="Basics.html#m:96"><span class="id" title="variable">m</span></a>.<br/>
</div>

<div class="doc">
Instead of making a universal claim about all numbers <span class="inlinecode"><span class="id" title="var">n</span></span> and <span class="inlinecode"><span class="id" title="var">m</span></span>,
    it talks about a more specialized property that only holds when
    <span class="inlinecode"><span class="id" title="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" title="var">m</span></span>.  The arrow symbol is pronounced "implies."

<div class="paragraph"> </div>

    As before, we need to be able to reason by assuming we are given such
    numbers <span class="inlinecode"><span class="id" title="var">n</span></span> and <span class="inlinecode"><span class="id" title="var">m</span></span>.  We also need to assume the hypothesis
    <span class="inlinecode"><span class="id" title="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" title="var">m</span></span>. The <span class="inlinecode"><span class="id" title="tactic">intros</span></span> tactic will serve to move all three of these
    from the goal into assumptions in the current context.

<div class="paragraph"> </div>

    Since <span class="inlinecode"><span class="id" title="var">n</span></span> and <span class="inlinecode"><span class="id" title="var">m</span></span> are arbitrary numbers, we can't just use
    simplification to prove this theorem.  Instead, we prove it by
    observing that, if we are assuming <span class="inlinecode"><span class="id" title="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" title="var">m</span></span>, then we can replace
    <span class="inlinecode"><span class="id" title="var">n</span></span> with <span class="inlinecode"><span class="id" title="var">m</span></span> in the goal statement and obtain an equality with the
    same expression on both sides.  The tactic that tells Coq to
    perform this replacement is called <span class="inlinecode"><span class="id" title="tactic">rewrite</span></span>. 
</div>
<div class="code">

<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="comment">(*&nbsp;move&nbsp;both&nbsp;quantifiers&nbsp;into&nbsp;the&nbsp;context:&nbsp;*)</span><br/>
&nbsp;&nbsp;<span class="id" title="tactic">intros</span> <span class="id" title="var">n</span> <span class="id" title="var">m</span>.<br/>
&nbsp;&nbsp;<span class="comment">(*&nbsp;move&nbsp;the&nbsp;hypothesis&nbsp;into&nbsp;the&nbsp;context:&nbsp;*)</span><br/>
&nbsp;&nbsp;<span class="id" title="tactic">intros</span> <span class="id" title="var">H</span>.<br/>
&nbsp;&nbsp;<span class="comment">(*&nbsp;rewrite&nbsp;the&nbsp;goal&nbsp;using&nbsp;the&nbsp;hypothesis:&nbsp;*)</span><br/>
&nbsp;&nbsp;<span class="id" title="tactic">rewrite</span> → <span class="id" title="var">H</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
The first line of the proof moves the universally quantified
    variables <span class="inlinecode"><span class="id" title="var">n</span></span> and <span class="inlinecode"><span class="id" title="var">m</span></span> into the context.  The second moves the
    hypothesis <span class="inlinecode"><span class="id" title="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" title="var">m</span></span> into the context and gives it the name <span class="inlinecode"><span class="id" title="var">H</span></span>.
    The third tells Coq to rewrite the current goal (<span class="inlinecode"><span class="id" title="var">n</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" title="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" title="var">m</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" title="var">m</span></span>)
    by replacing the left side of the equality hypothesis <span class="inlinecode"><span class="id" title="var">H</span></span> with the
    right side.

<div class="paragraph"> </div>

    (The arrow symbol in the <span class="inlinecode"><span class="id" title="tactic">rewrite</span></span> has nothing to do with
    implication: it tells Coq to apply the rewrite from left to right.
    In fact, you can omit the arrow, and Coq will default to rewriting
    in this direction.  To rewrite from right to left, you can use
    <span class="inlinecode"><span class="id" title="tactic">rewrite</span></span> <span class="inlinecode">&lt;-</span>.  Try making this change in the above proof and see
    what difference it makes.) 
<div class="paragraph"> </div>

<a id="lab36"></a><h4 class="section">Exercise: 1 star, standard (plus_id_exercise)</h4>
 Remove "<span class="inlinecode"><span class="id" title="var">Admitted</span>.</span>" and fill in the proof. 
</div>
<div class="code">

<span class="comment">(*&nbsp;SOOMER:&nbsp;KK:&nbsp;<span class="inlinecode"><span class="id" title="var">plus_id_exercise</span></span>&nbsp;contains&nbsp;multiple&nbsp;hypotheses,&nbsp;and&nbsp;at<br/>
&nbsp;&nbsp;&nbsp;least&nbsp;one&nbsp;student&nbsp;was&nbsp;confused&nbsp;about&nbsp;this.&nbsp;Maybe&nbsp;we&nbsp;can&nbsp;talk&nbsp;about<br/>
&nbsp;&nbsp;&nbsp;<span class="inlinecode">→</span>&nbsp;being&nbsp;right-associative&nbsp;before&nbsp;it.&nbsp;*)</span><br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Theorem</span> <a id="plus_id_exercise" class="idref" href="#plus_id_exercise"><span class="id" title="lemma">plus_id_exercise</span></a> : <span class="id" title="keyword">∀</span> <a id="n:97" class="idref" href="#n:97"><span class="id" title="binder">n</span></a> <a id="m:98" class="idref" href="#m:98"><span class="id" title="binder">m</span></a> <a id="o:99" class="idref" href="#o:99"><span class="id" title="binder">o</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>,<br/>
&nbsp;&nbsp;<a class="idref" href="Basics.html#n:97"><span class="id" title="variable">n</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#m:98"><span class="id" title="variable">m</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'-&gt;'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="Basics.html#m:98"><span class="id" title="variable">m</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#o:99"><span class="id" title="variable">o</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'-&gt;'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="Basics.html#n:97"><span class="id" title="variable">n</span></a> <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> <a class="idref" href="Basics.html#m:98"><span class="id" title="variable">m</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#m:98"><span class="id" title="variable">m</span></a> <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> <a class="idref" href="Basics.html#o:99"><span class="id" title="variable">o</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>&#9744;</font>
</div>

<div class="doc"> 
<div class="paragraph"> </div>

 The <span class="inlinecode"><span class="id" title="var">Admitted</span></span> command tells Coq that we want to skip trying
    to prove this theorem and just accept it as a given.  This can be
    useful for developing longer proofs, since we can state subsidiary
    lemmas that we believe will be useful for making some larger
    argument, use <span class="inlinecode"><span class="id" title="var">Admitted</span></span> to accept them on faith for the moment,
    and continue working on the main argument until we are sure it
    makes sense; then we can go back and fill in the proofs we
    skipped.  Be careful, though: every time you say <span class="inlinecode"><span class="id" title="var">Admitted</span></span> you
    are leaving a door open for total nonsense to enter Coq's nice,
    rigorous, formally checked world! 
<div class="paragraph"> </div>

 The <span class="inlinecode"><span class="id" title="keyword">Check</span></span> command can also be used to examine the statements of
    previously declared lemmas and theorems.  The two examples below
    are lemmas about multiplication that are proved in the standard
    library.  (We will see how to prove them ourselves in the next
    chapter.) 
</div>
<div class="code">

<span class="id" title="keyword">Check</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#mult_n_O"><span class="id" title="lemma">mult_n_O</span></a>.<br/>
<span class="comment">(*&nbsp;===&gt;&nbsp;forall&nbsp;n&nbsp;:&nbsp;nat,&nbsp;0&nbsp;=&nbsp;n&nbsp;*&nbsp;0&nbsp;*)</span><br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Check</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#mult_n_Sm"><span class="id" title="lemma">mult_n_Sm</span></a>.<br/>
<span class="comment">(*&nbsp;===&gt;&nbsp;forall&nbsp;n&nbsp;m&nbsp;:&nbsp;nat,&nbsp;n&nbsp;*&nbsp;m&nbsp;+&nbsp;n&nbsp;=&nbsp;n&nbsp;*&nbsp;S&nbsp;m&nbsp;*)</span><br/>
</div>

<div class="doc">
We can use the <span class="inlinecode"><span class="id" title="tactic">rewrite</span></span> tactic with a previously proved theorem
    instead of a hypothesis from the context. If the statement of the
    previously proved theorem involves quantified variables, as in the
    example below, Coq tries to instantiate them by matching with the
    current goal. 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="mult_n_0_m_0" class="idref" href="#mult_n_0_m_0"><span class="id" title="lemma">mult_n_0_m_0</span></a> : <span class="id" title="keyword">∀</span> <a id="p:100" class="idref" href="#p:100"><span class="id" title="binder">p</span></a> <a id="q:101" class="idref" href="#q:101"><span class="id" title="binder">q</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>,<br/>
&nbsp;&nbsp;<a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#p:100"><span class="id" title="variable">p</span></a> <a class="idref" href="Basics.html#ea2ff3d561159081cea6fb2e8113cc<sub>54</sub>"><span class="id" title="notation">×</span></a> 0<a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">)</span></a> <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#q:101"><span class="id" title="variable">q</span></a> <a class="idref" href="Basics.html#ea2ff3d561159081cea6fb2e8113cc<sub>54</sub>"><span class="id" title="notation">×</span></a> 0<a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> 0.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">intros</span> <span class="id" title="var">p</span> <span class="id" title="var">q</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">rewrite</span> &lt;- <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#mult_n_O"><span class="id" title="lemma">mult_n_O</span></a>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">rewrite</span> &lt;- <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#mult_n_O"><span class="id" title="lemma">mult_n_O</span></a>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
<a id="lab37"></a><h4 class="section">Exercise: 1 star, standard (mult_n_1)</h4>
 Use those two lemmas about multiplication that we just checked to
    prove the following theorem.  Hint: recall that <span class="inlinecode">1</span> is <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">O</span></span>. 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="mult_n_1" class="idref" href="#mult_n_1"><span class="id" title="lemma">mult_n_1</span></a> : <span class="id" title="keyword">∀</span> <a id="p:102" class="idref" href="#p:102"><span class="id" title="binder">p</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>,<br/>
&nbsp;&nbsp;<a class="idref" href="Basics.html#p:102"><span class="id" title="variable">p</span></a> <a class="idref" href="Basics.html#ea2ff3d561159081cea6fb2e8113cc<sub>54</sub>"><span class="id" title="notation">×</span></a> 1 <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#p:102"><span class="id" title="variable">p</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>&#9744;</font>
</div>


<div class="doc">
<a id="lab38"></a><h1 class="section">Proof by Case Analysis</h1>

<div class="paragraph"> </div>

 Of course, not everything can be proved by simple
    calculation and rewriting: In general, unknown, hypothetical
    values (arbitrary numbers, booleans, lists, etc.) can block
    simplification.  For example, if we try to prove the following
    fact using the <span class="inlinecode"><span class="id" title="tactic">simpl</span></span> tactic as above, we get stuck.  (We then
    use the <span class="inlinecode"><span class="id" title="keyword">Abort</span></span> command to give up on it for the moment.)
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="plus_1_neq_0_firsttry" class="idref" href="#plus_1_neq_0_firsttry"><span class="id" title="lemma">plus_1_neq_0_firsttry</span></a> : <span class="id" title="keyword">∀</span> <a id="n:103" class="idref" href="#n:103"><span class="id" title="binder">n</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>,<br/>
&nbsp;&nbsp;<a class="idref" href="Basics.html#ad2ec4e405f68c46c0a176e3e94ae2e<sub>3</sub>"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#n:103"><span class="id" title="variable">n</span></a> <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> 1<a class="idref" href="Basics.html#ad2ec4e405f68c46c0a176e3e94ae2e<sub>3</sub>"><span class="id" title="notation">)</span></a> <a class="idref" href="Basics.html#ad2ec4e405f68c46c0a176e3e94ae2e<sub>3</sub>"><span class="id" title="notation">=?</span></a> 0 <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">intros</span> <span class="id" title="var">n</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">simpl</span>. <span class="comment">(*&nbsp;does&nbsp;nothing!&nbsp;*)</span><br/>
<span class="id" title="keyword">Abort</span>.<br/>
</div>

<div class="doc">
The reason for this is that the definitions of both <span class="inlinecode"><span class="id" title="var">eqb</span></span>
    and <span class="inlinecode">+</span> begin by performing a <span class="inlinecode"><span class="id" title="keyword">match</span></span> on their first argument.
    But here, the first argument to <span class="inlinecode">+</span> is the unknown number <span class="inlinecode"><span class="id" title="var">n</span></span> and
    the argument to <span class="inlinecode"><span class="id" title="var">eqb</span></span> is the compound expression <span class="inlinecode"><span class="id" title="var">n</span></span> <span class="inlinecode">+</span> <span class="inlinecode">1</span>; neither
    can be simplified.

<div class="paragraph"> </div>

    To make progress, we need to consider the possible forms of <span class="inlinecode"><span class="id" title="var">n</span></span>
    separately.  If <span class="inlinecode"><span class="id" title="var">n</span></span> is <span class="inlinecode"><span class="id" title="var">O</span></span>, then we can calculate the final result
    of <span class="inlinecode">(<span class="id" title="var">n</span></span> <span class="inlinecode">+</span> <span class="inlinecode">1)</span> <span class="inlinecode">=?</span> <span class="inlinecode">0</span> and check that it is, indeed, <span class="inlinecode"><span class="id" title="var">false</span></span>.  And if
    <span class="inlinecode"><span class="id" title="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">n'</span></span> for some <span class="inlinecode"><span class="id" title="var">n'</span></span>, then, although we don't know exactly
    what number <span class="inlinecode"><span class="id" title="var">n</span></span> <span class="inlinecode">+</span> <span class="inlinecode">1</span> represents, we can calculate that, at least,
    it will begin with one <span class="inlinecode"><span class="id" title="var">S</span></span>, and this is enough to calculate that,
    again, <span class="inlinecode">(<span class="id" title="var">n</span></span> <span class="inlinecode">+</span> <span class="inlinecode">1)</span> <span class="inlinecode">=?</span> <span class="inlinecode">0</span> will yield <span class="inlinecode"><span class="id" title="var">false</span></span>.

<div class="paragraph"> </div>

    The tactic that tells Coq to consider, separately, the cases where
    <span class="inlinecode"><span class="id" title="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" title="var">O</span></span> and where <span class="inlinecode"><span class="id" title="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">n'</span></span> is called <span class="inlinecode"><span class="id" title="tactic">destruct</span></span>. 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="plus_1_neq_0" class="idref" href="#plus_1_neq_0"><span class="id" title="lemma">plus_1_neq_0</span></a> : <span class="id" title="keyword">∀</span> <a id="n:104" class="idref" href="#n:104"><span class="id" title="binder">n</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>,<br/>
&nbsp;&nbsp;<a class="idref" href="Basics.html#ad2ec4e405f68c46c0a176e3e94ae2e<sub>3</sub>"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#n:104"><span class="id" title="variable">n</span></a> <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> 1<a class="idref" href="Basics.html#ad2ec4e405f68c46c0a176e3e94ae2e<sub>3</sub>"><span class="id" title="notation">)</span></a> <a class="idref" href="Basics.html#ad2ec4e405f68c46c0a176e3e94ae2e<sub>3</sub>"><span class="id" title="notation">=?</span></a> 0 <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">intros</span> <span class="id" title="var">n</span>. <span class="id" title="tactic">destruct</span> <span class="id" title="var">n</span> <span class="id" title="keyword">as</span> [| <span class="id" title="var">n'</span>] <span class="id" title="var">eqn</span>:<span class="id" title="var">E</span>.<br/>
&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>.<br/>
&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
The <span class="inlinecode"><span class="id" title="tactic">destruct</span></span> generates <i>two</i> subgoals, which we must then
    prove, separately, in order to get Coq to accept the theorem.

<div class="paragraph"> </div>

    The annotation "<span class="inlinecode"><span class="id" title="keyword">as</span></span> <span class="inlinecode">[|</span> <span class="inlinecode"><span class="id" title="var">n'</span>]</span>" is called an <i>intro pattern</i>.  It
    tells Coq what variable names to introduce in each subgoal.  In
    general, what goes between the square brackets is a <i>list of
    lists</i> of names, separated by <span class="inlinecode">|</span>.  In this case, the first
    component is empty, since the <span class="inlinecode"><span class="id" title="var">O</span></span> constructor is nullary (it
    doesn't have any arguments).  The second component gives a single
    name, <span class="inlinecode"><span class="id" title="var">n'</span></span>, since <span class="inlinecode"><span class="id" title="var">S</span></span> is a unary constructor.

<div class="paragraph"> </div>

    In each subgoal, Coq remembers the assumption about <span class="inlinecode"><span class="id" title="var">n</span></span> that is
    relevant for this subgoal -- either <span class="inlinecode"><span class="id" title="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode">0</span> or <span class="inlinecode"><span class="id" title="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">n'</span></span> for some
    n'.  The <span class="inlinecode"><span class="id" title="var">eqn</span>:<span class="id" title="var">E</span></span> annotation tells <span class="inlinecode"><span class="id" title="tactic">destruct</span></span> to give the name <span class="inlinecode"><span class="id" title="var">E</span></span>
    to this equation.  Leaving off the <span class="inlinecode"><span class="id" title="var">eqn</span>:<span class="id" title="var">E</span></span> annotation causes Coq
    to elide these assumptions in the subgoals.  This slightly
    streamlines proofs where the assumptions are not explicitly used,
    but it is better practice to keep them for the sake of
    documentation, as they can help keep you oriented when working
    with the subgoals.

<div class="paragraph"> </div>

    The <span class="inlinecode">-</span> signs on the second and third lines are called <i>bullets</i>,
    and they mark the parts of the proof that correspond to the two
    generated subgoals.  The part of the proof script that comes after
    a bullet is the entire proof for the corresponding subgoal.  In
    this example, each of the subgoals is easily proved by a single
    use of <span class="inlinecode"><span class="id" title="tactic">reflexivity</span></span>, which itself performs some simplification --
    e.g., the second one simplifies <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">n'</span></span> <span class="inlinecode">+</span> <span class="inlinecode">1)</span> <span class="inlinecode">=?</span> <span class="inlinecode">0</span> to <span class="inlinecode"><span class="id" title="var">false</span></span> by
    first rewriting <span class="inlinecode">(<span class="id" title="var">S</span></span> <span class="inlinecode"><span class="id" title="var">n'</span></span> <span class="inlinecode">+</span> <span class="inlinecode">1)</span> to <span class="inlinecode"><span class="id" title="var">S</span></span> <span class="inlinecode">(<span class="id" title="var">n'</span></span> <span class="inlinecode">+</span> <span class="inlinecode">1)</span>, then unfolding
    <span class="inlinecode"><span class="id" title="var">eqb</span></span>, and then simplifying the <span class="inlinecode"><span class="id" title="keyword">match</span></span>.

<div class="paragraph"> </div>

    Marking cases with bullets is optional: if bullets are not
    present, Coq simply asks you to prove each subgoal in sequence,
    one at a time. But it is a good idea to use bullets.  For one
    thing, they make the structure of a proof apparent, improving
    readability. Also, bullets instruct Coq to ensure that a subgoal
    is complete before trying to verify the next one, preventing
    proofs for different subgoals from getting mixed up. These issues
    become especially important in large developments, where fragile
    proofs lead to long debugging sessions.

<div class="paragraph"> </div>

    There are no hard and fast rules for how proofs should be
    formatted in Coq -- e.g., where lines should be broken and how
    sections of the proof should be indented to indicate their nested
    structure.  However, if the places where multiple subgoals are
    generated are marked with explicit bullets at the beginning of
    lines, then the proof will be readable almost no matter what
    choices are made about other aspects of layout.

<div class="paragraph"> </div>

    This is also a good place to mention one other piece of somewhat
    obvious advice about line lengths.  Beginning Coq users sometimes
    tend to the extremes, either writing each tactic on its own line
    or writing entire proofs on a single line.  Good style lies
    somewhere in the middle.  One reasonable guideline is to limit
    yourself to 80-character lines.

<div class="paragraph"> </div>

    The <span class="inlinecode"><span class="id" title="tactic">destruct</span></span> tactic can be used with any inductively defined
    datatype.  For example, we use it next to prove that boolean
    negation is involutive -- i.e., that negation is its own
    inverse. 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="negb_involutive" class="idref" href="#negb_involutive"><span class="id" title="lemma">negb_involutive</span></a> : <span class="id" title="keyword">∀</span> <a id="b:105" class="idref" href="#b:105"><span class="id" title="binder">b</span></a> : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>,<br/>
&nbsp;&nbsp;<a class="idref" href="Basics.html#negb"><span class="id" title="definition">negb</span></a> (<a class="idref" href="Basics.html#negb"><span class="id" title="definition">negb</span></a> <a class="idref" href="Basics.html#b:105"><span class="id" title="variable">b</span></a>) <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#b:105"><span class="id" title="variable">b</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">intros</span> <span class="id" title="var">b</span>. <span class="id" title="tactic">destruct</span> <span class="id" title="var">b</span> <span class="id" title="var">eqn</span>:<span class="id" title="var">E</span>.<br/>
&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>.<br/>
&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
Note that the <span class="inlinecode"><span class="id" title="tactic">destruct</span></span> here has no <span class="inlinecode"><span class="id" title="keyword">as</span></span> clause because
    none of the subcases of the <span class="inlinecode"><span class="id" title="tactic">destruct</span></span> need to bind any variables,
    so there is no need to specify any names.  In fact, we can omit
    the <span class="inlinecode"><span class="id" title="keyword">as</span></span> clause from <i>any</i> <span class="inlinecode"><span class="id" title="tactic">destruct</span></span> and Coq will fill in
    variable names automatically.  This is generally considered bad
    style, since Coq often makes confusing choices of names when left
    to its own devices.

<div class="paragraph"> </div>

    It is sometimes useful to invoke <span class="inlinecode"><span class="id" title="tactic">destruct</span></span> inside a subgoal,
    generating yet more proof obligations. In this case, we use
    different kinds of bullets to mark goals on different "levels."
    For example: 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="andb_commutative" class="idref" href="#andb_commutative"><span class="id" title="lemma">andb_commutative</span></a> : <span class="id" title="keyword">∀</span> <a id="b:106" class="idref" href="#b:106"><span class="id" title="binder">b</span></a> <a id="c:107" class="idref" href="#c:107"><span class="id" title="binder">c</span></a>, <a class="idref" href="Basics.html#andb"><span class="id" title="definition">andb</span></a> <a class="idref" href="Basics.html#b:106"><span class="id" title="variable">b</span></a> <a class="idref" href="Basics.html#c:107"><span class="id" title="variable">c</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#andb"><span class="id" title="definition">andb</span></a> <a class="idref" href="Basics.html#c:107"><span class="id" title="variable">c</span></a> <a class="idref" href="Basics.html#b:106"><span class="id" title="variable">b</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">intros</span> <span class="id" title="var">b</span> <span class="id" title="var">c</span>. <span class="id" title="tactic">destruct</span> <span class="id" title="var">b</span> <span class="id" title="var">eqn</span>:<span class="id" title="var">Eb</span>.<br/>
&nbsp;&nbsp;- <span class="id" title="tactic">destruct</span> <span class="id" title="var">c</span> <span class="id" title="var">eqn</span>:<span class="id" title="var">Ec</span>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;+ <span class="id" title="tactic">reflexivity</span>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;+ <span class="id" title="tactic">reflexivity</span>.<br/>
&nbsp;&nbsp;- <span class="id" title="tactic">destruct</span> <span class="id" title="var">c</span> <span class="id" title="var">eqn</span>:<span class="id" title="var">Ec</span>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;+ <span class="id" title="tactic">reflexivity</span>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;+ <span class="id" title="tactic">reflexivity</span>.<br/>
<span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
Each pair of calls to <span class="inlinecode"><span class="id" title="tactic">reflexivity</span></span> corresponds to the
    subgoals that were generated after the execution of the <span class="inlinecode"><span class="id" title="tactic">destruct</span></span> <span class="inlinecode"><span class="id" title="var">c</span></span>
    line right above it. 
<div class="paragraph"> </div>

 Besides <span class="inlinecode">-</span> and <span class="inlinecode">+</span>, we can use <span class="inlinecode">×</span> (asterisk) or any repetition
    of a bullet symbol (e.g. <span class="inlinecode">--</span> or <span class="inlinecode">***</span>) as a bullet.  We can also
    enclose sub-proofs in curly braces: 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="andb_commutative'" class="idref" href="#andb_commutative'"><span class="id" title="lemma">andb_commutative'</span></a> : <span class="id" title="keyword">∀</span> <a id="b:108" class="idref" href="#b:108"><span class="id" title="binder">b</span></a> <a id="c:109" class="idref" href="#c:109"><span class="id" title="binder">c</span></a>, <a class="idref" href="Basics.html#andb"><span class="id" title="definition">andb</span></a> <a class="idref" href="Basics.html#b:108"><span class="id" title="variable">b</span></a> <a class="idref" href="Basics.html#c:109"><span class="id" title="variable">c</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#andb"><span class="id" title="definition">andb</span></a> <a class="idref" href="Basics.html#c:109"><span class="id" title="variable">c</span></a> <a class="idref" href="Basics.html#b:108"><span class="id" title="variable">b</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">intros</span> <span class="id" title="var">b</span> <span class="id" title="var">c</span>. <span class="id" title="tactic">destruct</span> <span class="id" title="var">b</span> <span class="id" title="var">eqn</span>:<span class="id" title="var">Eb</span>.<br/>
&nbsp;&nbsp;{ <span class="id" title="tactic">destruct</span> <span class="id" title="var">c</span> <span class="id" title="var">eqn</span>:<span class="id" title="var">Ec</span>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;{ <span class="id" title="tactic">reflexivity</span>. }<br/>
&nbsp;&nbsp;&nbsp;&nbsp;{ <span class="id" title="tactic">reflexivity</span>. } }<br/>
&nbsp;&nbsp;{ <span class="id" title="tactic">destruct</span> <span class="id" title="var">c</span> <span class="id" title="var">eqn</span>:<span class="id" title="var">Ec</span>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;{ <span class="id" title="tactic">reflexivity</span>. }<br/>
&nbsp;&nbsp;&nbsp;&nbsp;{ <span class="id" title="tactic">reflexivity</span>. } }<br/>
<span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
Since curly braces mark both the beginning and the end of a proof,
    they can be used for multiple subgoal levels, as this example
    shows. Furthermore, curly braces allow us to reuse the same bullet
    shapes at multiple levels in a proof. The choice of braces,
    bullets, or a combination of the two is purely a matter of
    taste. 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="andb3_exchange" class="idref" href="#andb3_exchange"><span class="id" title="lemma">andb3_exchange</span></a> :<br/>
&nbsp;&nbsp;<span class="id" title="keyword">∀</span> <a id="b:110" class="idref" href="#b:110"><span class="id" title="binder">b</span></a> <a id="c:111" class="idref" href="#c:111"><span class="id" title="binder">c</span></a> <a id="d:112" class="idref" href="#d:112"><span class="id" title="binder">d</span></a>, <a class="idref" href="Basics.html#andb"><span class="id" title="definition">andb</span></a> (<a class="idref" href="Basics.html#andb"><span class="id" title="definition">andb</span></a> <a class="idref" href="Basics.html#b:110"><span class="id" title="variable">b</span></a> <a class="idref" href="Basics.html#c:111"><span class="id" title="variable">c</span></a>) <a class="idref" href="Basics.html#d:112"><span class="id" title="variable">d</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#andb"><span class="id" title="definition">andb</span></a> (<a class="idref" href="Basics.html#andb"><span class="id" title="definition">andb</span></a> <a class="idref" href="Basics.html#b:110"><span class="id" title="variable">b</span></a> <a class="idref" href="Basics.html#d:112"><span class="id" title="variable">d</span></a>) <a class="idref" href="Basics.html#c:111"><span class="id" title="variable">c</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">intros</span> <span class="id" title="var">b</span> <span class="id" title="var">c</span> <span class="id" title="var">d</span>. <span class="id" title="tactic">destruct</span> <span class="id" title="var">b</span> <span class="id" title="var">eqn</span>:<span class="id" title="var">Eb</span>.<br/>
&nbsp;&nbsp;- <span class="id" title="tactic">destruct</span> <span class="id" title="var">c</span> <span class="id" title="var">eqn</span>:<span class="id" title="var">Ec</span>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;{ <span class="id" title="tactic">destruct</span> <span class="id" title="var">d</span> <span class="id" title="var">eqn</span>:<span class="id" title="var">Ed</span>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>. }<br/>
&nbsp;&nbsp;&nbsp;&nbsp;{ <span class="id" title="tactic">destruct</span> <span class="id" title="var">d</span> <span class="id" title="var">eqn</span>:<span class="id" title="var">Ed</span>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>. }<br/>
&nbsp;&nbsp;- <span class="id" title="tactic">destruct</span> <span class="id" title="var">c</span> <span class="id" title="var">eqn</span>:<span class="id" title="var">Ec</span>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;{ <span class="id" title="tactic">destruct</span> <span class="id" title="var">d</span> <span class="id" title="var">eqn</span>:<span class="id" title="var">Ed</span>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>. }<br/>
&nbsp;&nbsp;&nbsp;&nbsp;{ <span class="id" title="tactic">destruct</span> <span class="id" title="var">d</span> <span class="id" title="var">eqn</span>:<span class="id" title="var">Ed</span>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>. }<br/>
<span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
<a id="lab39"></a><h4 class="section">Exercise: 2 stars, standard (andb_true_elim2)</h4>
 Prove the following claim, marking cases (and subcases) with
    bullets when you use <span class="inlinecode"><span class="id" title="tactic">destruct</span></span>. Hint: delay introducing the
    hypothesis until after you have an opportunity to simplify it. 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="andb_true_elim2" class="idref" href="#andb_true_elim2"><span class="id" title="lemma">andb_true_elim2</span></a> : <span class="id" title="keyword">∀</span> <a id="b:113" class="idref" href="#b:113"><span class="id" title="binder">b</span></a> <a id="c:114" class="idref" href="#c:114"><span class="id" title="binder">c</span></a> : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>,<br/>
&nbsp;&nbsp;<a class="idref" href="Basics.html#andb"><span class="id" title="definition">andb</span></a> <a class="idref" href="Basics.html#b:113"><span class="id" title="variable">b</span></a> <a class="idref" href="Basics.html#c:114"><span class="id" title="variable">c</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'-&gt;'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="Basics.html#c:114"><span class="id" title="variable">c</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#true"><span class="id" title="constructor">true</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>&#9744;</font>
</div>

<div class="doc"> 
<div class="paragraph"> </div>

 Before closing the chapter, let's mention one final
    convenience.  As you may have noticed, many proofs perform case
    analysis on a variable right after introducing it:
<br/>
<span class="inlinecode">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="id" title="tactic">intros</span> <span class="id" title="var">x</span> <span class="id" title="var">y</span>. <span class="id" title="tactic">destruct</span> <span class="id" title="var">y</span> <span class="id" title="keyword">as</span> [|<span class="id" title="var">y</span>] <span class="id" title="var">eqn</span>:<span class="id" title="var">E</span>.
</span>    This pattern is so common that Coq provides a shorthand for it: we
    can perform case analysis on a variable when introducing it by
    using an intro pattern instead of a variable name. For instance,
    here is a shorter proof of the <span class="inlinecode"><span class="id" title="var">plus_1_neq_0</span></span> theorem
    above.  (You'll also note one downside of this shorthand: we lose
    the equation recording the assumption we are making in each
    subgoal, which we previously got from the <span class="inlinecode"><span class="id" title="var">eqn</span>:<span class="id" title="var">E</span></span> annotation.) 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="plus_1_neq_0'" class="idref" href="#plus_1_neq_0'"><span class="id" title="lemma">plus_1_neq_0'</span></a> : <span class="id" title="keyword">∀</span> <a id="n:115" class="idref" href="#n:115"><span class="id" title="binder">n</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>,<br/>
&nbsp;&nbsp;<a class="idref" href="Basics.html#ad2ec4e405f68c46c0a176e3e94ae2e<sub>3</sub>"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#n:115"><span class="id" title="variable">n</span></a> <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> 1<a class="idref" href="Basics.html#ad2ec4e405f68c46c0a176e3e94ae2e<sub>3</sub>"><span class="id" title="notation">)</span></a> <a class="idref" href="Basics.html#ad2ec4e405f68c46c0a176e3e94ae2e<sub>3</sub>"><span class="id" title="notation">=?</span></a> 0 <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">intros</span> [|<span class="id" title="var">n</span>].<br/>
&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>.<br/>
&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
If there are no constructor arguments that need names, we can just
    write <span class="inlinecode">[]</span> to get the case analysis. 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="andb_commutative''" class="idref" href="#andb_commutative''"><span class="id" title="lemma">andb_commutative''</span></a> :<br/>
&nbsp;&nbsp;<span class="id" title="keyword">∀</span> <a id="b:116" class="idref" href="#b:116"><span class="id" title="binder">b</span></a> <a id="c:117" class="idref" href="#c:117"><span class="id" title="binder">c</span></a>, <a class="idref" href="Basics.html#andb"><span class="id" title="definition">andb</span></a> <a class="idref" href="Basics.html#b:116"><span class="id" title="variable">b</span></a> <a class="idref" href="Basics.html#c:117"><span class="id" title="variable">c</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#andb"><span class="id" title="definition">andb</span></a> <a class="idref" href="Basics.html#c:117"><span class="id" title="variable">c</span></a> <a class="idref" href="Basics.html#b:116"><span class="id" title="variable">b</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="id" title="tactic">intros</span> [] [].<br/>
&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>.<br/>
&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>.<br/>
&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>.<br/>
&nbsp;&nbsp;- <span class="id" title="tactic">reflexivity</span>.<br/>
<span class="id" title="keyword">Qed</span>.<br/>
</div>

<div class="doc">
<a id="lab40"></a><h4 class="section">Exercise: 1 star, standard (zero_nbeq_plus_1)</h4>

</div>
<div class="code">
<span class="id" title="keyword">Theorem</span> <a id="zero_nbeq_plus_1" class="idref" href="#zero_nbeq_plus_1"><span class="id" title="lemma">zero_nbeq_plus_1</span></a> : <span class="id" title="keyword">∀</span> <a id="n:118" class="idref" href="#n:118"><span class="id" title="binder">n</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>,<br/>
&nbsp;&nbsp;0 <a class="idref" href="Basics.html#ad2ec4e405f68c46c0a176e3e94ae2e<sub>3</sub>"><span class="id" title="notation">=?</span></a> <a class="idref" href="Basics.html#ad2ec4e405f68c46c0a176e3e94ae2e<sub>3</sub>"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#n:118"><span class="id" title="variable">n</span></a> <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> 1<a class="idref" href="Basics.html#ad2ec4e405f68c46c0a176e3e94ae2e<sub>3</sub>"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#false"><span class="id" title="constructor">false</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>&#9744;</font>
</div>


<div class="doc">
<a id="lab41"></a><h2 class="section">More on Notation (Optional)</h2>

<div class="paragraph"> </div>

 (In general, sections marked Optional are not needed to follow the
    rest of the book, except possibly other Optional sections.  On a
    first reading, you might want to skim these sections so that you
    know what's there for future reference.)

<div class="paragraph"> </div>

    Recall the notation definitions for infix plus and times: 
</div>
<div class="code">

<span class="id" title="keyword">Notation</span> <a id="0dacc1786c5ba797d47dd85006231633" class="idref" href="#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">&quot;</span></a>x + y" := (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#plus"><span class="id" title="abbreviation">plus</span></a> <span class="id" title="var">x</span> <span class="id" title="var">y</span>)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span class="id" title="tactic">at</span> <span class="id" title="keyword">level</span> 50, <span class="id" title="tactic">left</span> <span class="id" title="keyword">associativity</span>)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span class="id" title="var">nat_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="ea2ff3d561159081cea6fb2e8113cc<sub>54</sub>" class="idref" href="#ea2ff3d561159081cea6fb2e8113cc<sub>54</sub>"><span class="id" title="notation">&quot;</span></a>x * y" := (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#mult"><span class="id" title="abbreviation">mult</span></a> <span class="id" title="var">x</span> <span class="id" title="var">y</span>)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span class="id" title="tactic">at</span> <span class="id" title="keyword">level</span> 40, <span class="id" title="tactic">left</span> <span class="id" title="keyword">associativity</span>)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span class="id" title="var">nat_scope</span>.<br/>
</div>

<div class="doc">
For each notation symbol in Coq, we can specify its <i>precedence
    level</i> and its <i>associativity</i>.  The precedence level <span class="inlinecode"><span class="id" title="var">n</span></span> is
    specified by writing <span class="inlinecode"><span class="id" title="tactic">at</span></span> <span class="inlinecode"><span class="id" title="keyword">level</span></span> <span class="inlinecode"><span class="id" title="var">n</span></span>; this helps Coq parse compound
    expressions.  The associativity setting helps to disambiguate
    expressions containing multiple occurrences of the same
    symbol. For example, the parameters specified above for <span class="inlinecode">+</span> and
    <span class="inlinecode">×</span> say that the expression <span class="inlinecode">1+2*3*4</span> is shorthand for
    <span class="inlinecode">(1+((2*3)*4))</span>. Coq uses precedence levels from 0 to 100, and
    <i>left</i>, <i>right</i>, or <i>no</i> associativity.  We will see more examples
    of this later, e.g., in the <a href="Lists.html"><span class="inlinecode"><span class="id" title="var">Lists</span></span></a>
    chapter.

<div class="paragraph"> </div>

    Each notation symbol is also associated with a <i>notation scope</i>.
    Coq tries to guess what scope is meant from context, so when it
    sees <span class="inlinecode"><span class="id" title="var">S</span>(<span class="id" title="var">O</span>×<span class="id" title="var">O</span>)</span> it guesses <span class="inlinecode"><span class="id" title="var">nat_scope</span></span>, but when it sees the product
    type <span class="inlinecode"><span class="id" title="var">bool</span>×<span class="id" title="var">bool</span></span> (which we'll see in later chapters) it guesses
    <span class="inlinecode"><span class="id" title="var">type_scope</span></span>.  Occasionally, it is necessary to help it out with
    percent-notation by writing <span class="inlinecode">(<span class="id" title="var">x</span>×<span class="id" title="var">y</span>)%<span class="id" title="var">nat</span></span>, and sometimes in what Coq
    prints it will use <span class="inlinecode">%<span class="id" title="var">nat</span></span> to indicate what scope a notation is in.

<div class="paragraph"> </div>

    Notation scopes also apply to numeral notation (<span class="inlinecode">3</span>, <span class="inlinecode">4</span>, <span class="inlinecode">5</span>, <span class="inlinecode">42</span>,
    etc.), so you may sometimes see <span class="inlinecode">0%<span class="id" title="var">nat</span></span>, which means <span class="inlinecode"><span class="id" title="var">O</span></span> (the
    natural number <span class="inlinecode">0</span> that we're using in this chapter), or <span class="inlinecode">0%<span class="id" title="var">Z</span></span>,
    which means the integer zero (which comes from a different part of
    the standard library).

<div class="paragraph"> </div>

    Pro tip: Coq's notation mechanism is not especially powerful.
    Don't expect too much from it. 
</div>

<div class="doc">
<a id="lab42"></a><h2 class="section">Fixpoints and Structural Recursion (Optional)</h2>

<div class="paragraph"> </div>

 Here is a copy of the definition of addition: 
</div>
<div class="code">

<span class="id" title="keyword">Fixpoint</span> <a id="plus'" class="idref" href="#plus'"><span class="id" title="definition">plus'</span></a> (<a id="n:119" class="idref" href="#n:119"><span class="id" title="binder">n</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) (<a id="m:120" class="idref" href="#m:120"><span class="id" title="binder">m</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a> :=<br/>
&nbsp;&nbsp;<span class="id" title="keyword">match</span> <a class="idref" href="Basics.html#n:119"><span class="id" title="variable">n</span></a> <span class="id" title="keyword">with</span><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#O"><span class="id" title="constructor">O</span></a> ⇒ <a class="idref" href="Basics.html#m:120"><span class="id" title="variable">m</span></a><br/>
&nbsp;&nbsp;| <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="var">n'</span> ⇒ <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> (<a class="idref" href="Basics.html#plus':121"><span class="id" title="definition">plus'</span></a> <span class="id" title="var">n'</span> <a class="idref" href="Basics.html#m:120"><span class="id" title="variable">m</span></a>)<br/>
&nbsp;&nbsp;<span class="id" title="keyword">end</span>.<br/>
</div>

<div class="doc">
When Coq checks this definition, it notes that <span class="inlinecode"><span class="id" title="var">plus'</span></span> is
    "decreasing on 1st argument."  What this means is that we are
    performing a <i>structural recursion</i> over the argument <span class="inlinecode"><span class="id" title="var">n</span></span> -- i.e.,
    that we make recursive calls only on strictly smaller values of
    <span class="inlinecode"><span class="id" title="var">n</span></span>.  This implies that all calls to <span class="inlinecode"><span class="id" title="var">plus'</span></span> will eventually
    terminate.  Coq demands that some argument of <i>every</i> <span class="inlinecode"><span class="id" title="keyword">Fixpoint</span></span>
    definition is "decreasing."

<div class="paragraph"> </div>

    This requirement is a fundamental feature of Coq's design: In
    particular, it guarantees that every function that can be defined
    in Coq will terminate on all inputs.  However, because Coq's
    "decreasing analysis" is not very sophisticated, it is sometimes
    necessary to write functions in slightly unnatural ways. 
<div class="paragraph"> </div>

<a id="lab43"></a><h4 class="section">Exercise: 2 stars, standard, optional (decreasing)</h4>
 To get a concrete sense of this, find a way to write a sensible
    <span class="inlinecode"><span class="id" title="keyword">Fixpoint</span></span> definition (of a simple function on numbers, say) that
    <i>does</i> terminate on all inputs, but that Coq will reject because
    of this restriction.  (If you choose to turn in this optional
    exercise as part of a homework assignment, make sure you comment
    out your solution so that it doesn't cause Coq to reject the whole
    file!) 
</div>
<div class="code">

<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span><br/>
<font size=-2>&#9744;</font>
</div>


<div class="doc">
<a id="lab44"></a><h1 class="section">More Exercises</h1>

<div class="paragraph"> </div>

<a id="lab45"></a><h4 class="section">Exercise: 1 star, standard (identity_fn_applied_twice)</h4>
 Use the tactics you have learned so far to prove the following
    theorem about boolean functions. 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="identity_fn_applied_twice" class="idref" href="#identity_fn_applied_twice"><span class="id" title="lemma">identity_fn_applied_twice</span></a> :<br/>
&nbsp;&nbsp;<span class="id" title="keyword">∀</span> (<a id="f:123" class="idref" href="#f:123"><span class="id" title="binder">f</span></a> : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'-&gt;'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>),<br/>
&nbsp;&nbsp;<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'-&gt;'_x"><span class="id" title="notation">(</span></a><span class="id" title="keyword">∀</span> (<a id="x:124" class="idref" href="#x:124"><span class="id" title="binder">x</span></a> : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>), <a class="idref" href="Basics.html#f:123"><span class="id" title="variable">f</span></a> <a class="idref" href="Basics.html#x:124"><span class="id" title="variable">x</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#x:124"><span class="id" title="variable">x</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'-&gt;'_x"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'-&gt;'_x"><span class="id" title="notation">→</span></a><br/>
&nbsp;&nbsp;<span class="id" title="keyword">∀</span> (<a id="b:125" class="idref" href="#b:125"><span class="id" title="binder">b</span></a> : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>), <a class="idref" href="Basics.html#f:123"><span class="id" title="variable">f</span></a> (<a class="idref" href="Basics.html#f:123"><span class="id" title="variable">f</span></a> <a class="idref" href="Basics.html#b:125"><span class="id" title="variable">b</span></a>) <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#b:125"><span class="id" title="variable">b</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>&#9744;</font>
</div>

<div class="doc"> 
<div class="paragraph"> </div>

<a id="lab46"></a><h4 class="section">Exercise: 1 star, standard (negation_fn_applied_twice)</h4>
 Now state and prove a theorem <span class="inlinecode"><span class="id" title="var">negation_fn_applied_twice</span></span> similar
    to the previous one but where the second hypothesis says that the
    function <span class="inlinecode"><span class="id" title="var">f</span></span> has the property that <span class="inlinecode"><span class="id" title="var">f</span></span> <span class="inlinecode"><span class="id" title="var">x</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" title="var">negb</span></span> <span class="inlinecode"><span class="id" title="var">x</span></span>. 
</div>
<div class="code">

<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span><br/><hr class='doublespaceincode'/>
<span class="comment">(*&nbsp;Do&nbsp;not&nbsp;modify&nbsp;the&nbsp;following&nbsp;line:&nbsp;*)</span><br/>
<span class="id" title="keyword">Definition</span> <a id="manual_grade_for_negation_fn_applied_twice" class="idref" href="#manual_grade_for_negation_fn_applied_twice"><span class="id" title="definition">manual_grade_for_negation_fn_applied_twice</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#option"><span class="id" title="inductive">option</span></a> (<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#11c698c8685bb8ab1cf725545c085ac<sub>4</sub>"><span class="id" title="notation">×</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Strings.String.html#string"><span class="id" title="inductive">string</span></a>) := <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#None"><span class="id" title="constructor">None</span></a>.<br/>
</div>

<div class="doc">
(The last definition is used by the autograder.)  <font size=-2>&#9744;</font> 
<div class="paragraph"> </div>

<a id="lab47"></a><h4 class="section">Exercise: 3 stars, standard, optional (andb_eq_orb)</h4>
 Prove the following theorem.  (Hint: This one can be a bit tricky,
    depending on how you approach it.  You will probably need both
    <span class="inlinecode"><span class="id" title="tactic">destruct</span></span> and <span class="inlinecode"><span class="id" title="tactic">rewrite</span></span>, but destructing everything in sight is
    not the best way.) 
</div>
<div class="code">

<span class="id" title="keyword">Theorem</span> <a id="andb_eq_orb" class="idref" href="#andb_eq_orb"><span class="id" title="lemma">andb_eq_orb</span></a> :<br/>
&nbsp;&nbsp;<span class="id" title="keyword">∀</span> (<a id="b:126" class="idref" href="#b:126"><span class="id" title="binder">b</span></a> <a id="c:127" class="idref" href="#c:127"><span class="id" title="binder">c</span></a> : <a class="idref" href="Basics.html#bool"><span class="id" title="inductive">bool</span></a>),<br/>
&nbsp;&nbsp;<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'-&gt;'_x"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#andb"><span class="id" title="definition">andb</span></a> <a class="idref" href="Basics.html#b:126"><span class="id" title="variable">b</span></a> <a class="idref" href="Basics.html#c:127"><span class="id" title="variable">c</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#orb"><span class="id" title="definition">orb</span></a> <a class="idref" href="Basics.html#b:126"><span class="id" title="variable">b</span></a> <a class="idref" href="Basics.html#c:127"><span class="id" title="variable">c</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'-&gt;'_x"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'-&gt;'_x"><span class="id" title="notation">→</span></a><br/>
&nbsp;&nbsp;<a class="idref" href="Basics.html#b:126"><span class="id" title="variable">b</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#c:127"><span class="id" title="variable">c</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
&nbsp;&nbsp;<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>&#9744;</font>
</div>

<div class="doc"> 
<div class="paragraph"> </div>

<a id="lab48"></a><h4 class="section">Exercise: 3 stars, standard (binary)</h4>
 We can generalize our unary representation of natural numbers to
    the more efficient binary representation by treating a binary
    number as a sequence of constructors <span class="inlinecode"><span class="id" title="var">B<sub>0</sub></span></span> and <span class="inlinecode"><span class="id" title="var">B<sub>1</sub></span></span> (representing 0s
    and 1s), terminated by a <span class="inlinecode"><span class="id" title="var">Z</span></span>. For comparison, in the unary
    representation, a number is a sequence of <span class="inlinecode"><span class="id" title="var">S</span></span> constructors terminated
    by an <span class="inlinecode"><span class="id" title="var">O</span></span>.

<div class="paragraph"> </div>

    For example:
<pre>
        decimal               binary                          unary
           0                       Z                              O
           1                    B<sub>1</sub> Z                            S O
           2                B<sub>0</sub> (B<sub>1</sub> Z)                        S (S O)
           3                B<sub>1</sub> (B<sub>1</sub> Z)                     S (S (S O))
           4            B<sub>0</sub> (B<sub>0</sub> (B<sub>1</sub> Z))                 S (S (S (S O)))
           5            B<sub>1</sub> (B<sub>0</sub> (B<sub>1</sub> Z))              S (S (S (S (S O))))
           6            B<sub>0</sub> (B<sub>1</sub> (B<sub>1</sub> Z))           S (S (S (S (S (S O)))))
           7            B<sub>1</sub> (B<sub>1</sub> (B<sub>1</sub> Z))        S (S (S (S (S (S (S O))))))
           8        B<sub>0</sub> (B<sub>0</sub> (B<sub>0</sub> (B<sub>1</sub> Z)))    S (S (S (S (S (S (S (S O)))))))
</pre>
    Note that the low-order bit is on the left and the high-order bit
    is on the right -- the opposite of the way binary numbers are
    usually written.  This choice makes them easier to manipulate. 
</div>
<div class="code">

<span class="id" title="keyword">Inductive</span> <a id="bin" class="idref" href="#bin"><span class="id" title="inductive">bin</span></a> : <span class="id" title="keyword">Type</span> :=<br/>
&nbsp;&nbsp;| <a id="Z" class="idref" href="#Z"><span class="id" title="constructor">Z</span></a><br/>
&nbsp;&nbsp;| <a id="B<sub>0</sub>" class="idref" href="#B<sub>0</sub>"><span class="id" title="constructor">B<sub>0</sub></span></a> (<a id="n:130" class="idref" href="#n:130"><span class="id" title="binder">n</span></a> : <a class="idref" href="Basics.html#bin:128"><span class="id" title="inductive">bin</span></a>)<br/>
&nbsp;&nbsp;| <a id="B<sub>1</sub>" class="idref" href="#B<sub>1</sub>"><span class="id" title="constructor">B<sub>1</sub></span></a> (<a id="n:131" class="idref" href="#n:131"><span class="id" title="binder">n</span></a> : <a class="idref" href="Basics.html#bin:128"><span class="id" title="inductive">bin</span></a>).<br/>
</div>

<div class="doc">
Complete the definitions below of an increment function <span class="inlinecode"><span class="id" title="var">incr</span></span>
    for binary numbers, and a function <span class="inlinecode"><span class="id" title="var">bin_to_nat</span></span> to convert
    binary numbers to unary numbers. 
</div>
<div class="code">

<span class="id" title="keyword">Fixpoint</span> <a id="incr" class="idref" href="#incr"><span class="id" title="definition">incr</span></a> (<a id="m:132" class="idref" href="#m:132"><span class="id" title="binder">m</span></a>:<a class="idref" href="Basics.html#bin"><span class="id" title="inductive">bin</span></a>) : <a class="idref" href="Basics.html#bin"><span class="id" title="inductive">bin</span></a><br/>
&nbsp;&nbsp;<span class="comment">(*&nbsp;REPLACE&nbsp;THIS&nbsp;LINE&nbsp;WITH&nbsp;":=&nbsp;_your_definition_&nbsp;."&nbsp;*)</span>. <span class="id" title="var">Admitted</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Fixpoint</span> <a id="bin_to_nat" class="idref" href="#bin_to_nat"><span class="id" title="definition">bin_to_nat</span></a> (<a id="m:134" class="idref" href="#m:134"><span class="id" title="binder">m</span></a>:<a class="idref" href="Basics.html#bin"><span class="id" title="inductive">bin</span></a>) : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a><br/>
&nbsp;&nbsp;<span class="comment">(*&nbsp;REPLACE&nbsp;THIS&nbsp;LINE&nbsp;WITH&nbsp;":=&nbsp;_your_definition_&nbsp;."&nbsp;*)</span>. <span class="id" title="var">Admitted</span>.<br/>
</div>

<div class="doc">
The following "unit tests" of your increment and binary-to-unary
    functions should pass after you have defined those functions correctly.
    Of course, unit tests don't fully demonstrate the correctness of
    your functions!  We'll return to that thought at the end of the
    next chapter. 
</div>
<div class="code">

<span class="id" title="keyword">Example</span> <a id="test_bin_incr1" class="idref" href="#test_bin_incr1"><span class="id" title="definition">test_bin_incr1</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#incr"><span class="id" title="axiom">incr</span></a> (<a class="idref" href="Basics.html#B<sub>1</sub>"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#Z"><span class="id" title="constructor">Z</span></a>)<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#B<sub>0</sub>"><span class="id" title="constructor">B<sub>0</sub></span></a> (<a class="idref" href="Basics.html#B<sub>1</sub>"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#Z"><span class="id" title="constructor">Z</span></a>).<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="test_bin_incr2" class="idref" href="#test_bin_incr2"><span class="id" title="definition">test_bin_incr2</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#incr"><span class="id" title="axiom">incr</span></a> (<a class="idref" href="Basics.html#B<sub>0</sub>"><span class="id" title="constructor">B<sub>0</sub></span></a> (<a class="idref" href="Basics.html#B<sub>1</sub>"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#Z"><span class="id" title="constructor">Z</span></a>))<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#B<sub>1</sub>"><span class="id" title="constructor">B<sub>1</sub></span></a> (<a class="idref" href="Basics.html#B<sub>1</sub>"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#Z"><span class="id" title="constructor">Z</span></a>).<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="test_bin_incr3" class="idref" href="#test_bin_incr3"><span class="id" title="definition">test_bin_incr3</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">(</span></a><a class="idref" href="Basics.html#incr"><span class="id" title="axiom">incr</span></a> (<a class="idref" href="Basics.html#B<sub>1</sub>"><span class="id" title="constructor">B<sub>1</sub></span></a> (<a class="idref" href="Basics.html#B<sub>1</sub>"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#Z"><span class="id" title="constructor">Z</span></a>))<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Basics.html#B<sub>0</sub>"><span class="id" title="constructor">B<sub>0</sub></span></a> (<a class="idref" href="Basics.html#B<sub>0</sub>"><span class="id" title="constructor">B<sub>0</sub></span></a> (<a class="idref" href="Basics.html#B<sub>1</sub>"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#Z"><span class="id" title="constructor">Z</span></a>)).<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="test_bin_incr4" class="idref" href="#test_bin_incr4"><span class="id" title="definition">test_bin_incr4</span></a> : <a class="idref" href="Basics.html#bin_to_nat"><span class="id" title="axiom">bin_to_nat</span></a> (<a class="idref" href="Basics.html#B<sub>0</sub>"><span class="id" title="constructor">B<sub>0</sub></span></a> (<a class="idref" href="Basics.html#B<sub>1</sub>"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#Z"><span class="id" title="constructor">Z</span></a>)) <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> 2.<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="test_bin_incr5" class="idref" href="#test_bin_incr5"><span class="id" title="definition">test_bin_incr5</span></a> :<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class="idref" href="Basics.html#bin_to_nat"><span class="id" title="axiom">bin_to_nat</span></a> (<a class="idref" href="Basics.html#incr"><span class="id" title="axiom">incr</span></a> (<a class="idref" href="Basics.html#B<sub>1</sub>"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#Z"><span class="id" title="constructor">Z</span></a>)) <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> 1 <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> <a class="idref" href="Basics.html#bin_to_nat"><span class="id" title="axiom">bin_to_nat</span></a> (<a class="idref" href="Basics.html#B<sub>1</sub>"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#Z"><span class="id" title="constructor">Z</span></a>).<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Example</span> <a id="test_bin_incr6" class="idref" href="#test_bin_incr6"><span class="id" title="definition">test_bin_incr6</span></a> :<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class="idref" href="Basics.html#bin_to_nat"><span class="id" title="axiom">bin_to_nat</span></a> (<a class="idref" href="Basics.html#incr"><span class="id" title="axiom">incr</span></a> (<a class="idref" href="Basics.html#incr"><span class="id" title="axiom">incr</span></a> (<a class="idref" href="Basics.html#B<sub>1</sub>"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#Z"><span class="id" title="constructor">Z</span></a>))) <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> 2 <a class="idref" href="Basics.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> <a class="idref" href="Basics.html#bin_to_nat"><span class="id" title="axiom">bin_to_nat</span></a> (<a class="idref" href="Basics.html#B<sub>1</sub>"><span class="id" title="constructor">B<sub>1</sub></span></a> <a class="idref" href="Basics.html#Z"><span class="id" title="constructor">Z</span></a>).<br/>
<span class="comment">(*&nbsp;FILL&nbsp;IN&nbsp;HERE&nbsp;*)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>&#9744;</font>
</div>


<div class="doc">
<a id="lab49"></a><h1 class="section">Testing Your Solutions</h1>

<div class="paragraph"> </div>

 Each SF chapter comes with a test file containing scripts that
    check whether you have solved the required exercises. If you're
    using SF as part of a course, your instructors will likely be
    running these test files to autograde your solutions. You can also
    use these test files, if you like, to make sure you haven't missed
    anything.

<div class="paragraph"> </div>

    Important: This step is <i>optional</i>: if you've completed all the
    non-optional exercises and Coq accepts your answers, this already
    shows that you are in good shape.

<div class="paragraph"> </div>

    The test file for this chapter is <span class="inlinecode"><span class="id" title="var">BasicsTest.v</span></span>. To run it, make
    sure you have saved <span class="inlinecode"><span class="id" title="var">Basics.v</span></span> to disk.  Then do this:
<br/>
<span class="inlinecode">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="id" title="var">coqc</span> -<span class="id" title="var">Q</span> . <span class="id" title="var">LF</span> <span class="id" title="var">Basics.v</span><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="id" title="var">coqc</span> -<span class="id" title="var">Q</span> . <span class="id" title="var">LF</span> <span class="id" title="var">BasicsTest.v</span>
</span>
<div class="paragraph"> </div>

    If you accidentally deleted an exercise or changed its name, then
    <span class="inlinecode"><span class="id" title="var">make</span></span> <span class="inlinecode"><span class="id" title="var">BasicsTest.vo</span></span> will fail with an error that tells you the
    name of the missing exercise.  Otherwise, you will get a lot of
    useful output:

<div class="paragraph"> </div>

<ul class="doclist">
<li> First will be all the output produced by <span class="inlinecode"><span class="id" title="var">Basics.v</span></span> itself.  At
      the end of that you will see <span class="inlinecode"><span class="id" title="var">COQC</span></span> <span class="inlinecode"><span class="id" title="var">BasicsTest.v</span></span>.

<div class="paragraph"> </div>


</li>
<li> Second, for each required exercise, there is a report that tells
      you its point value (the number of stars or some fraction
      thereof if there are multiple parts to the exercise), whether
      its type is ok, and what assumptions it relies upon.

<div class="paragraph"> </div>

      If the <i>type</i> is not <span class="inlinecode"><span class="id" title="var">ok</span></span>, it means you proved the wrong thing:
      most likely, you accidentally modified the theorem statement
      while you were proving it.  The autograder won't give you any
      points for that, so make sure to correct the theorem.

<div class="paragraph"> </div>

      The <i>assumptions</i> are any unproved theorems which your solution
      relies upon.  "Closed under the global context" is a fancy way
      of saying "none": you have solved the exercise. (Hooray!)  On
      the other hand, a list of axioms means you haven't fully solved
      the exercise. (But see below regarding "Allowed Axioms.") If the
      exercise name itself is in the list, that means you haven't
      solved it; probably you have <span class="inlinecode"><span class="id" title="var">Admitted</span></span> it.

<div class="paragraph"> </div>


</li>
<li> Third, you will see the maximum number of points in standard and
      advanced versions of the assignment.  That number is based on
      the number of stars in the non-optional exercises.

<div class="paragraph"> </div>


</li>
<li> Fourth, you will see a list of "Allowed Axioms".  These are
      unproved theorems that your solution is permitted to depend
      upon.  You'll probably see something about
      <span class="inlinecode"><span class="id" title="var">functional_extensionality</span></span> for this chapter; we'll cover what
      that means in a later chapter.

<div class="paragraph"> </div>


</li>
<li> Finally, you will see a summary of whether you have solved each
      exercise.  Note that summary does not include the critical
      information of whether the type is ok (that is, whether you
      accidentally changed the theorem statement): you have to look
      above for that information.

</li>
</ul>

<div class="paragraph"> </div>

    Exercises that are manually graded will also show up in the
    output.  But since they have to be graded by a human, the test
    script won't be able to tell you much about them.  
</div>
<div class="code">

<span class="comment">(*&nbsp;2021-08-11&nbsp;15:08&nbsp;*)</span><br/>
</div>
</div>

<div id="footer">
<hr/><a href="coqindex.html">Index</a><hr/>This page has been generated by <a href="http://coq.inria.fr/">coqdoc</a>
</div>

</div>

</body>
</html>